1 /*globals Handlebars */ 2 3 sc_require('ext/handlebars'); 4 5 Handlebars.registerHelper('collection', function(path, options) { 6 var fn = options.fn; 7 var data = options.data; 8 var inverse = options.inverse; 9 var hash = options.hash; 10 var collectionClass, collectionObject; 11 12 collectionClass = path ? SC.getPath(this, path) || SC.getPath(path) : 13 SC.TemplateCollectionView; 14 15 // @if (debug) 16 if (!collectionClass) { 17 throw new Error("%@ #collection: Could not find %@".fmt(data.view, path)); 18 } else if (!SC.kindOf(collectionClass, SC.TemplateCollectionView)) { 19 throw new Error("You must use a subclass of SC.TemplateCollectionView when using the #collection Handlebars helper"); 20 } 21 // @endif 22 23 var extensions = {}; 24 25 if (hash) { 26 var itemHash = {}, match; 27 28 for (var prop in hash) { 29 if (hash.hasOwnProperty(prop)) { 30 match = prop.match(/^item(.)(.*)$/); 31 32 if(match) { 33 itemHash[match[1].toLowerCase() + match[2]] = hash[prop]; 34 delete hash[prop]; 35 } 36 } 37 } 38 39 extensions = SC.clone(hash); 40 extensions.itemViewOptions = itemHash; 41 // don't duplicate properties in the view helper 42 options.hash = {}; 43 } 44 45 if (fn) { extensions.itemViewTemplate = fn; } 46 if (inverse) { extensions.inverseTemplate = inverse; } 47 48 if(collectionClass.isClass) { 49 collectionObject = collectionClass.extend(extensions); 50 } else { 51 collectionObject = SC.mixin(collectionClass, extensions); 52 } 53 54 options.fn = function() { return ""; }; 55 56 return Handlebars.helpers.view.call(this, collectionObject, options); 57 }); 58 59 Handlebars.registerHelper('each', function(path, options) { 60 options.hash.contentBinding = SC.Binding.from('*'+path, this).oneWay(); 61 options.hash.itemContextProperty = 'content'; 62 return Handlebars.helpers.collection.call(this, null, options); 63 }); 64