Skip to content
Browse files

addChildren implemented in Container

  • Loading branch information...
1 parent 03164d8 commit f3e41bd0f09e3659efce8630da70077a49a373a5 @robrobbins robrobbins committed
Showing with 42 additions and 7 deletions.
  1. +18 −3 build/debug/sudo.js
  2. +18 −3 container/container.js
  3. +6 −1 container/spec/container.spec.js
View
21 build/debug/sudo.js
@@ -339,13 +339,18 @@ sudo.Model.prototype.unsets = function unsets(ary) {
// ##Container Class Object
//
// A container is any object that can both contain other objects and
-// itself be contained
+// itself be contained.
+//
+// `param` {Array|Object} 'arg'. Optional array or hash
+// of child objects which the Container will add as child objects
+// via `addChildren`
//
// `constructor`
-sudo.Container = function() {
+sudo.Container = function(arg) {
sudo.Base.call(this);
this.children = [];
this.childNames = {};
+ if(arg) this.addChildren(arg);
};
// Container is a subclass of sudo.Base
sudo.inherit(sudo.Base, sudo.Container);
@@ -369,7 +374,15 @@ sudo.Container.prototype.addChild = function addChild(child, name) {
if('addedToParent' in child) child.addedToParent(this);
return this;
};
-// Comment
+// ###addChildren
+// Allows for multiple children to be added to this Container by passing
+// either an Array or an Object literal.
+//
+// see `addChild`
+//
+// `param` {Array|Object} `arg`. An array of children to add or an
+// Object literal in the form {name: child}
+// `returns` {Object} `this`
sudo.Container.prototype.addChildren = function addChildren(arg) {
var i, keys;
// Array?
@@ -444,6 +457,8 @@ sudo.Container.prototype.removeChild = function removeChild(arg) {
// Remove all children, name references and adjust indexes accordingly.
// This method calls removeFromParent as each child may have overridden logic there.
//
+// see `removeChild`
+//
// `returns` {object} `this`
sudo.Container.prototype.removeChildren = function removeChildren() {
while(this.children.length) {
View
21 container/container.js
@@ -1,13 +1,18 @@
// ##Container Class Object
//
// A container is any object that can both contain other objects and
-// itself be contained
+// itself be contained.
+//
+// `param` {Array|Object} 'arg'. Optional array or hash
+// of child objects which the Container will add as child objects
+// via `addChildren`
//
// `constructor`
-sudo.Container = function() {
+sudo.Container = function(arg) {
sudo.Base.call(this);
this.children = [];
this.childNames = {};
+ if(arg) this.addChildren(arg);
};
// Container is a subclass of sudo.Base
sudo.inherit(sudo.Base, sudo.Container);
@@ -31,7 +36,15 @@ sudo.Container.prototype.addChild = function addChild(child, name) {
if('addedToParent' in child) child.addedToParent(this);
return this;
};
-// Comment
+// ###addChildren
+// Allows for multiple children to be added to this Container by passing
+// either an Array or an Object literal.
+//
+// see `addChild`
+//
+// `param` {Array|Object} `arg`. An array of children to add or an
+// Object literal in the form {name: child}
+// `returns` {Object} `this`
sudo.Container.prototype.addChildren = function addChildren(arg) {
var i, keys;
// Array?
@@ -106,6 +119,8 @@ sudo.Container.prototype.removeChild = function removeChild(arg) {
// Remove all children, name references and adjust indexes accordingly.
// This method calls removeFromParent as each child may have overridden logic there.
//
+// see `removeChild`
+//
// `returns` {object} `this`
sudo.Container.prototype.removeChildren = function removeChildren() {
while(this.children.length) {
View
7 container/spec/container.spec.js
@@ -31,7 +31,7 @@ describe('Sudo Container Class', function() {
expect(container.children.length).toBe(2);
});
- it('Adds many children via an array', function() {
+ it('Adds many children via an object literal', function() {
var obj = {'Galahad': child1, 'Robin': child2};
container.addChildren(obj);
expect(container.children.length).toBe(2);
@@ -39,6 +39,11 @@ describe('Sudo Container Class', function() {
expect(container.getChild('Robin')).toBeTruthy();
});
+ it('Adds children passed to the constructor', function() {
+ var c = new _.Container([child1, child2]);
+ expect(c.children.length).toBe(2);
+ });
+
it('Can fetch a child by index or name', function() {
container.addChild(child1, 'Galahad').addChild(child2, 'Robin');
expect(container.getChild(0).uid).toBe(cid1);

0 comments on commit f3e41bd

Please sign in to comment.
Something went wrong with that request. Please try again.