Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created April 20, 2011 16:17
Show Gist options
  • Save ColinCampbell/931804 to your computer and use it in GitHub Desktop.
Save ColinCampbell/931804 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 57e3cd2..e163501 100644
--- a/frameworks/core_foundation/ext/handlebars/collection.js
+++ b/frameworks/core_foundation/ext/handlebars/collection.js
@@ -1,3 +1,5 @@
+/*globals Handlebars */
+
sc_require('ext/handlebars');
Handlebars.registerHelper('collection', function(path, options) {
@@ -13,36 +15,39 @@ Handlebars.registerHelper('collection', function(path, options) {
}
//@ endif
- var hash = fn.hash, itemHash = {}, match;
+ var extensions = {};
+
+ if (fn) {
+ var hash = fn.hash, itemHash = {}, match;
- for (var prop in hash) {
- if (fn.hash.hasOwnProperty(prop)) {
- match = prop.match(/^item(.)(.*)$/);
+ for (var prop in hash) {
+ if (fn.hash.hasOwnProperty(prop)) {
+ match = prop.match(/^item(.)(.*)$/);
- if(match) {
- itemHash[match[1].toLowerCase() + match[2]] = hash[prop];
- delete hash[prop];
+ if(match) {
+ itemHash[match[1].toLowerCase() + match[2]] = hash[prop];
+ delete hash[prop];
+ }
}
}
- }
- if(fn) {
- var extensions = SC.clone(hash);
+ extensions = SC.clone(hash);
SC.mixin(extensions, {
itemViewTemplate: fn,
inverseTemplate: inverse,
itemViewOptions: itemHash
});
+ }
- if(collectionClass.isClass) {
- collectionObject = collectionClass.extend(extensions);
- } else {
- SC.mixin(collectionClass, extensions);
- collectionObject = collectionClass;
- }
+ if(collectionClass.isClass) {
+ collectionObject = collectionClass.extend(extensions);
+ } else {
+ SC.mixin(collectionClass, extensions);
}
+ collectionObject = collectionClass;
+
options.fn = function() { return ""; };
return Handlebars.helpers.view.call(this, collectionObject, options);
diff --git a/frameworks/core_foundation/tests/views/template/collection.js b/frameworks/core_foundation/tests/views/template/collection.js
index 212beb5..2df8b22 100644
--- a/frameworks/core_foundation/tests/views/template/collection.js
+++ b/frameworks/core_foundation/tests/views/template/collection.js
@@ -4,6 +4,8 @@
// ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
+/*globals TemplateTests */
+
module("SC.TemplateCollectionView");
TemplateTests = {};
@@ -24,7 +26,7 @@ test("creating a collection view works", function() {
var ulCollectionView = CollectionView.create({ tagName: "ul" });
var olCollectionView = CollectionView.create({ tagName: "ol" });
var dlCollectionView = CollectionView.create({ tagName: "dl", itemView: DefinitionTermChildView });
- var customTagCollectionView = CollectionView.create({ tagName: "p" })
+ var customTagCollectionView = CollectionView.create({ tagName: "p" });
defaultCollectionView.createLayer();
ulCollectionView.createLayer();
@@ -48,6 +50,22 @@ test("creating a collection view works", function() {
equals(customTagCollectionView.$('div').length, 1, "Child view was rendered");
});
+test("not passing a block to the collection helper creates a collection", function() {
+ TemplateTests.CollectionTestView = SC.TemplateCollectionView.create({
+ content: ['foo', 'bar', 'baz'],
+ itemView: SC.TemplateView.design({
+ template: SC.Handlebars.compile('<aside></aside>')
+ })
+ });
+
+ var view = SC.TemplateView.create({
+ template: SC.Handlebars.compile('{{collection "TemplateTests.CollectionTestView"}}')
+ });
+
+ view.createLayer();
+ equals(view.$('aside').length, 3, 'one aside element is created for each content item');
+});
+
test("passing a block to the collection helper sets it as the template for example views", function() {
TemplateTests.CollectionTestView = SC.TemplateCollectionView.create({
content: ['foo', 'bar', 'baz']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment