Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Forked from wagenet/1-helper.js
Created May 3, 2012 19:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlivsey/2588802 to your computer and use it in GitHub Desktop.
Save rlivsey/2588802 to your computer and use it in GitHub Desktop.
Ember Handlebars Transform Helper
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);
};
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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment