JSFiddle - React, Tailwind, and code Playground
by jrab227
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
<div ng-app="application">
<div ng-controller="myCtr1">
</div>
</div>
JavaScript
angular.module('application', [])
.factory('Diskshelf', function() {
function Diskshelf(params) {
this.params = params;
this.disks = 0;
return;
}
Diskshelf.prototype.addDisk = function(success, error) {
if (this.disks < this.params.diskLimit) {
this.disks += 1;
success();
return this;
}
error();
return this;
}
return Diskshelf;
})
.factory('Node', function() {
function Node(params) {
this.params = params;
this.diskshelves = [];
return;
}
Node.prototype.addDiskshelf = function(diskshelf, success, error) {
if (this.diskshelves.length < this.params.diskshelfLimit) {
this.diskshelves.push(diskshelf);
success();
return this;
}
error();
return this;
}
Node.prototype.addDisk = function(success, error) {
this.diskshelves = _.reduce(this.diskshelves, function(diskshelves, diskshelf) {
if (!diskshelves.alreadyAddedDisk) {
diskshelf.addDisk(function() {
diskshelves.alreadyAddedDisk = true;
}, function() {
return;
});
}
diskshelves.push(diskshelf);
return diskshelves;
}, []);
if (!this.diskshelves.alreadyAddedDisk) {
error();
return this;
}
success();
return this;
}
return Node;
})
.controller('myCtr1', ['$scope', 'Node', 'Diskshelf', function($scope, Node, Diskshelf) {
var diskshelf1 = new Diskshelf({
diskLimit: 2
})
var diskshelf2 = new Diskshelf({
diskLimit: 4
})
var node1 = new Node({
diskshelfLimit: 2
});
function success(){
console.log("successful")
}
function error(){
console.log("something happened")
}
node1.addDiskshelf(diskshelf1, success, error)
.addDiskshelf(diskshelf2, success, error)
.addDisk(success, error)
...