Created
January 5, 2012 04:25
-
-
Save wagenet/1563710 to your computer and use it in GitHub Desktop.
Ember Handlebars Transform Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
I have been using this snippet for quite some time, and I'm currently trying to upgrade to the 1.0 pre code. I've got everything else working except this bit of code, and I haven't been able to figure out what has changed, or even if this code is still needed. In line 1 I've tried mixing in Ember._Metamorph and Ember._MetamorphView but to no avail. Any suggestions?