Created
March 11, 2012 20:55
-
-
Save ghempton/2018185 to your computer and use it in GitHub Desktop.
Ember Bound Handlebars Helper Utility
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
// This file contains utilities for creating bound helpers | |
// For reference: https://github.com/wagenet/ember.js/blob/ac66dcb8a1cbe91d736074441f853e0da474ee6e/packages/ember-handlebars/lib/views/bound_property_view.js | |
Ember.Handlebars.BoundHelperView = Ember.View.extend(Ember._Metamorph, { | |
context: null, | |
options: null, | |
property: null, | |
// paths of the property that are also observed | |
propertyPaths: [], | |
value: Ember.K, | |
valueForRender: function() { | |
var value = this.value(Ember.Handlebars.getPath(this.context, this.property, this.options), this.options.hash); | |
if (this.options.hash.escaped) { value = Handlebars.Utils.escapeExpression(value); } | |
return value; | |
}, | |
render: function(buffer) { | |
buffer.push(this.valueForRender()); | |
}, | |
valueDidChange: function() { | |
if (this.morph.isRemoved()) { return; } | |
this.morph.html(this.valueForRender()); | |
}, | |
didInsertElement: function() { | |
this.valueDidChange(); | |
}, | |
init: function() { | |
this._super(); | |
Ember.addObserver(this.context, this.property, this, 'valueDidChange'); | |
this.get('propertyPaths').forEach(function(propName) { | |
Ember.addObserver(this.context, this.property + '.' + propName, this, 'valueDidChange'); | |
}, this); | |
}, | |
destroy: function() { | |
Ember.removeObserver(this.context, this.property, this, 'valueDidChange'); | |
this.get('propertyPaths').forEach(function(propName) { | |
this.context.removeObserver(this.property + '.' + propName, this, 'valueDidChange'); | |
}, this); | |
this._super(); | |
} | |
}); | |
Ember.Handlebars.registerBoundHelper = function(name, func) { | |
var propertyPaths = Array.prototype.slice.call(arguments, 2); | |
Ember.Handlebars.registerHelper(name, function(property, options) { | |
var data = options.data, | |
view = data.view, | |
ctx = this; | |
var bindView = view.createChildView(GT.Handlebars.BoundHelperView, { | |
property: property, | |
propertyPaths: propertyPaths, | |
context: ctx, | |
options: options, | |
value: func | |
}); | |
view.appendChild(bindView); | |
}); | |
}; |
Its now Ember._Metamorph
. I have update the gist.
Gotcha, just found the same. Also, it seems that with ENV.VIEW_PRESERVES_CONTEXT = true
it seems to look at the wrong context?
I haven't looked into the view context change yet. I will investigate that
later.
On Mon, May 14, 2012 at 4:14 PM, Sven Fuchs < ***@***.*** > wrote:
Gotcha, just found the same. Also, it seems that with
`ENV.VIEW_PRESERVES_CONTEXT = true` it seems to look at the wrong context?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2018185
##
Gordon L. Hempton
http://codebrief.com
360.460.8098
@svenfuchs @ghempton I believe line 13 should be:
var value = this.value(Ember.Handlebars.getPath(this.context, this.property, this.options), this.options);
GT.Handlebars.BoundHelperView
should be Ember.Handlebars.BoundHelperView
@wagenet Fixed in this fork: https://gist.github.com/3789135
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems something has changed in Ember, since
this.morph
in line 15 is alwaysundefined
for me? any pointer?