Permalink
Browse files

Centralize lookup of itemTemplate / emptyTemplate, remove emptyContex…

…t option
  • Loading branch information...
1 parent 21d052d commit 3b93ec1694fa68ecf633c39cf3943f13e0efd046 Ryan Eastridge committed Dec 27, 2012
Showing with 32 additions and 20 deletions.
  1. +8 −7 src/collection.js
  2. +17 −8 src/helpers/collection.js
  3. +1 −0 src/util.js
  4. +6 −5 test/src/collection.js
View
@@ -148,25 +148,26 @@ _.extend(Thorax.View.prototype, {
},
emptyClass: 'empty',
renderEmpty: function() {
- var context = this.emptyContext ? this.emptyContext.call(this) : this.context();
if (this.emptyView) {
- var view = Thorax.Util.getViewInstance(this.emptyView, {});
+ var viewOptions = {};
if (this.emptyTemplate) {
- view.render(this.renderTemplate(this.emptyTemplate, context));
- } else {
- view.render();
+ viewOptions.template = this.emptyTemplate;
}
+ var view = Thorax.Util.getViewInstance(this.emptyView, viewOptions);
+ view.ensureRendered();
return view;
} else {
- return this.emptyTemplate && this.renderTemplate(this.emptyTemplate, context);
+ return this.emptyTemplate && this.renderTemplate(this.emptyTemplate);
}
},
renderItem: function(model, i) {
if (this.itemView) {
var viewOptions = {
model: model
};
- this.itemTemplate && (viewOptions.template = this.itemTemplate);
+ if (this.itemTemplate) {
+ viewOptions.template = this.itemTemplate;
+ }
var view = Thorax.Util.getViewInstance(this.itemView, viewOptions);
view.ensureRendered();
return view;
View
@@ -7,7 +7,13 @@ Thorax.CollectionHelperView = Thorax.View.extend({
},
constructor: function(options) {
_.each(collectionOptionNames, function(viewAttributeName, helperOptionName) {
- options.options[helperOptionName] && (options[viewAttributeName] = options.options[helperOptionName]);
+ if (options.options[helperOptionName]) {
+ var value = options.options[helperOptionName];
+ if (viewAttributeName === 'itemTemplate' || viewAttributeName === 'emptyTemplate') {
+ value = Thorax.Util.getTemplate(value);
+ }
+ options[viewAttributeName] = value;
+ }
});
// Handlebars.VM.noop is passed in the handlebars options object as
// a default for fn and inverse, if a block was present. Need to
@@ -23,21 +29,24 @@ Thorax.CollectionHelperView = Thorax.View.extend({
!options.template && (options.template = Handlebars.VM.noop);
var response = Thorax.CollectionHelperView.__super__.constructor.call(this, options);
if (this.parent.name) {
- this.emptyTemplate = this.emptyTemplate || Thorax.Util.getTemplate(this.parent.name + '-empty', true);
- this.itemTemplate = this.itemTemplate || Thorax.Util.getTemplate(this.parent.name + '-item', true);
+ if (!this.emptyTemplate) {
+ this.emptyTemplate = Thorax.Util.getTemplate(this.parent.name + '-empty', true);
+ }
+ if (!this.itemTemplate) {
+ this.itemTemplate = Thorax.Util.getTemplate(this.parent.name + '-item', true);
+ }
}
return response;
},
+ // will be used by emptyView and emptyTemplate
+ _getContext: function(attributes) {
+ return _.extend({}, getValue(this.parent, 'context'), attributes || {});
+ },
setAsPrimaryCollectionHelper: function(collection) {
this.$el.attr(primaryCollectionAttributeName, collection.cid);
_.each(forwardableProperties, function(propertyName) {
forwardMissingProperty.call(this, propertyName);
}, this);
- // emptyContext needs to be forced because it has a default
- forwardMissingProperty.call(this, 'emptyContext', true);
- },
- emptyContext: function() {
- return getValue(this.parent, 'context');
}
});
View
@@ -109,6 +109,7 @@ function getOptionsData(options) {
Thorax.Util = {
getViewInstance: function(name, attributes) {
+ attributes = attributes || {};
attributes['class'] && (attributes.className = attributes['class']);
attributes.tag && (attributes.tagName = attributes.tag);
if (typeof name === 'string') {
View
@@ -150,6 +150,11 @@ describe('collection', function() {
});
runCollectionTests(viewWithCollectionHelperWithEmptyView, 1, 'block helper with item-template');
+ var viewWithCollectionHelperWithItemViewAndItemTemplate = new Thorax.View({
+ template: '{{collection tag="ul" empty-view="letter-empty" item-view="letter-item" item-template="letter-item"}}'
+ });
+ runCollectionTests(viewWithCollectionHelperWithItemViewAndItemTemplate, 1, 'block helper with item-template');
+
var viewWithCollectionHelperWithEmptyViewAndBlock = new Thorax.View({
template: '{{collection tag="ul" empty-template="letter-empty" empty-view="letter-empty" item-template="letter-item"}}'
});
@@ -582,11 +587,7 @@ describe('collection', function() {
}
}))(),
template: "{{#collection this.collection}}{{test}}{{else}}<b>{{test}}</b>{{/collection}}",
- emptyContext: function() {
- return {
- test: 'testing'
- };
- }
+ test: 'testing'
});
view.render();
expect(view.$('b')[0].innerHTML).to.equal('testing');

0 comments on commit 3b93ec1

Please sign in to comment.