public
Created — forked from wagenet/1-helper.js

Ember Handlebars Transform Helper

  • Download Gist
1-helper.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Ember.HandlebarsTransformView = Ember.View.extend(Ember._Metamorph, {
rawValue: null,
transformFunc: null,
 
value: function(){
var rawValue = this.get('rawValue'),
transformFunc = this.get('transformFunc');
return transformFunc(rawValue);
}.property('rawValue', 'transformFunc').cacheable(),
 
render: function(buffer) {
var value = this.get('value');
if (value) { buffer.push(value); }
},
 
needsRerender: function() {
if (this.get("state") !== "destroyed") {
this.rerender();
}
}.observes('value')
});
 
Ember.HandlebarsTransformView.helper = function(context, property, transformFunc, options) {
options.hash = {
rawValueBinding: property,
transformFunc: transformFunc
};
return Ember.Handlebars.ViewHelper.helper(context, Ember.HandlebarsTransformView, options);
};
2-examples.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Ember.Handlebars.registerHelper('format', function(property, options) {
var transformFunc = function(value) {
return (value && value.format) ? value.format() : value;
};
return Ember.HandlebarsTransformView.helper(this, property, transformFunc, options);
});
 
// This one is untested but should work
Ember.Handlebars.registerHelper('datetime', function(property, options) {
var format = options.hash.format,
transformFunc = function(value) {
return (value && value.format) ? value.format(format) : value;
};
return Ember.HandlebarsTransformView.helper(this, property, transformFunc, options);
});
 
/*
* Example:
*
* var now = moment().add('days', 9);
* {{datetime now format="dddd, MMMM Do YYYY"}}
* Friday, January 13th 2012
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.