Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created April 28, 2011 02:40
Show Gist options
  • Save ColinCampbell/945689 to your computer and use it in GitHub Desktop.
Save ColinCampbell/945689 to your computer and use it in GitHub Desktop.
diff --git a/frameworks/core_foundation/ext/handlebars/collection.js b/frameworks/core_foundation/ext/handlebars/collection.js
index b790649..b7c094c 100644
--- a/frameworks/core_foundation/ext/handlebars/collection.js
+++ b/frameworks/core_foundation/ext/handlebars/collection.js
@@ -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;
@@ -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) {
@@ -32,11 +33,13 @@ Handlebars.registerHelper('collection', function(path, options) {
}
extensions = SC.clone(hash);
+ extensions.itemViewOptions = itemHash;
+ }
+ if (fn) {
SC.mixin(extensions, {
itemViewTemplate: fn,
- inverseTemplate: inverse,
- itemViewOptions: itemHash
+ inverseTemplate: inverse
});
}
diff --git a/frameworks/core_foundation/tests/views/template/collection.js b/frameworks/core_foundation/tests/views/template/collection.js
index 2df8b22..d4d024f 100644
--- a/frameworks/core_foundation/tests/views/template/collection.js
+++ b/frameworks/core_foundation/tests/views/template/collection.js
@@ -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({
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment