Skip to content

Commit

Permalink
Fixes sproutcore#398. Use options.hash instead of fn.hash and clean u…
Browse files Browse the repository at this point in the history
…p collection extension logic + unit tests
  • Loading branch information
Colin Campbell committed Apr 28, 2011
1 parent f437cab commit 314efd5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
17 changes: 8 additions & 9 deletions frameworks/core_foundation/ext/handlebars/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Handlebars.registerHelper('collection', function(path, options) {
var fn = options.fn;
var data = options.data;
var inverse = options.inverse;
var hash = options.hash;
var collectionClass, collectionObject;

collectionClass = path ? SC.objectForPropertyPath(path) : SC.TemplateCollectionView;
Expand All @@ -17,11 +18,11 @@ Handlebars.registerHelper('collection', function(path, options) {

var extensions = {};

if (fn) {
var hash = fn.hash, itemHash = {}, match;
if (hash) {
var itemHash = {}, match;

for (var prop in hash) {
if (fn.hash.hasOwnProperty(prop)) {
if (hash.hasOwnProperty(prop)) {
match = prop.match(/^item(.)(.*)$/);

if(match) {
Expand All @@ -32,14 +33,12 @@ Handlebars.registerHelper('collection', function(path, options) {
}

extensions = SC.clone(hash);

SC.mixin(extensions, {
itemViewTemplate: fn,
inverseTemplate: inverse,
itemViewOptions: itemHash
});
extensions.itemViewOptions = itemHash;
}

if (fn) { extensions.itemViewTemplate = fn; }
if (inverse) { extensions.inverseTemplate = inverse; }

if(collectionClass.isClass) {
collectionObject = collectionClass.extend(extensions);
} else {
Expand Down
30 changes: 30 additions & 0 deletions frameworks/core_foundation/tests/views/template/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,36 @@ test("should give its item views the classBinding specified by itemClassBinding"
equals(view.$('ul li.is-baz').length, 2, "removes class when property changes");
});

test("should pass item* property when created with a block", function() {
TemplateTests.CollectionTestView = SC.TemplateCollectionView.create({
content: ['foo', 'bar', 'baz']
});
var view = SC.TemplateView.create({
template: SC.Handlebars.compile('{{#collection TemplateTests.CollectionTestView itemFoo="bar"}}baz{{/collection}}')
});
view.createLayer();

var childViews = view.getPath('childViews.firstObject.childViews');
childViews.forEach(function(childView, index) {
equals(childView.get('foo'), 'bar', "Child view #%@ has correct value for property set in template".fmt(index));
});
});

test("should pass item* property when created without a block", function() {
TemplateTests.CollectionTestView = SC.TemplateCollectionView.create({
content: ['foo', 'bar', 'baz']
});
var view = SC.TemplateView.create({
template: SC.Handlebars.compile('{{collection TemplateTests.CollectionTestView itemFoo="bar"}}')
});
view.createLayer();

var childViews = view.getPath('childViews.firstObject.childViews');
childViews.forEach(function(childView, index) {
equals(childView.get('foo'), 'bar', "Child view #%@ has correct value for property set in template".fmt(index));
});
});

test("should work inside a bound {{#if}}", function() {
var testData = [SC.Object.create({ isBaz: false }), SC.Object.create({ isBaz: true }), SC.Object.create({ isBaz: true })];
TemplateTests.ifTestCollectionView = SC.TemplateCollectionView.extend({
Expand Down

0 comments on commit 314efd5

Please sign in to comment.