Skip to content
Browse files

Container accepts childViews at construction

Example:

  new Container({
    childViews: [
      new View({ content: 'Hello' })
    ]
  });
  • Loading branch information...
1 parent 295be59 commit 681ddf6adf909c78664f5cb14c41d522199df305 @seanmonstar committed Jul 25, 2012
Showing with 23 additions and 5 deletions.
  1. +11 −5 lib/shipyard/view/Container.js
  2. +12 −0 test/unit/view/container.js
View
16 lib/shipyard/view/Container.js
@@ -6,24 +6,25 @@ var Class = require('../class/Class'),
var SLICE = Array.prototype.slice;
-module.exports = new Class({
+var Container = module.exports = new Class({
Extends: View,
- childViews: [],
+ tag: 'div',
classNames: ['shipyard-container'],
- tag: 'div',
-
template: require('./templates/container.ejs'),
+ childViews: [],
+
initialize: function Container(params) {
var container = this;
var childViews = new ObservableArray();
childViews.observe('array', function onArrayChange() {
container.__onChildViewsChange.apply(container, arguments);
});
+ this.childViews = childViews;
this.set('childViews', childViews);
this.parent(params);
},
@@ -53,7 +54,7 @@ module.exports = new Class({
return object.merge(this.parent(), {
//views: this.childViews,
children: function children() {
- return views.map(function(child) {
+ return views.map(function children_map_render(child) {
return child.render();
}, this).join('');
}
@@ -96,5 +97,10 @@ module.exports = new Class({
}
+});
+Container.defineSetter('childViews', function(arr) {
+ var childViews = this.get('childViews');
+ var args = [0, childViews.length].concat(SLICE.call(arr));
+ childViews.splice.apply(childViews, args);
});
View
12 test/unit/view/container.js
@@ -40,6 +40,18 @@ module.exports = {
expect(c.toElement().getFirst().get('tag')).toBe(v2.get('tag'));
});
+
+ it('should be able to initialize with childViews', function(expect) {
+ var c = new Container({
+ childViews: [
+ new View({ id: 'derp' }),
+ new View({ id: 'herp' })
+ ]
+ });
+
+ expect(c.get('childViews')[0].get('parentView')).toBe(c);
+ expect(c.toElement().getFirst().get('id')).toBe('derp');
+ });
}
};

0 comments on commit 681ddf6

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