我正在尝试使用Handlebars帮助程序,但帮助程序视图不会更新。
观点,
<script type="text/x-handlebars">
{{#view App.NumberView}}
<button {{action changeNumber}}>Change number</button><br>
{{formatNumber view.number}} <br>
{{view.number}}
{{/view}}
</script>
代码,
App = Ember.Application.create({});
App.NumberView = Ember.View.extend({
number: 5,
changeNumber: function(e) {
this.set('number', this.get('number') + 1);
}
});
Em.Handlebars.registerHelper('formatNumber', function(timePath, options) {
var number = Em.Handlebars.getPath(this, timePath, options);
return new Handlebars.SafeString("Formated number: " + number);
});
jsfiddle http://jsfiddle.net/LP7Hz/1/
中的实例那有什么不对?
答案 0 :(得分:3)
因为Handlebars在Ember中帮助没有绑定帮助者。所以你可以实例化一个辅助视图,就像这个小提琴一样:
http://jsfiddle.net/secretlm/qfNJw/2/
HTML:
<script type="text/x-handlebars">
{{#view App.NumberView}}
<button {{action changeNumber}}>Change number</button><br>
{{formatNumber view.number}} <br>
{{view.number}}
{{/view}}
</script>
使用Javascript:
App = Ember.Application.create({});
App.NumberView = Ember.View.extend({
number: 5,
changeNumber: function(e) {
this.set('number', this.get('number') + 1);
}
});
App.registerViewHelper = function(name, view) {
Ember.Handlebars.registerHelper(name, function(property, options) {
options.hash.contentBinding = property;
return Ember.Handlebars.helpers.view.call(this, view, options);
});
};
inlineFormatter = function(fn) {
return Ember.View.extend({
tagName: 'span',
template: Ember.Handlebars.compile('{{view.formattedContent}}'),
formattedContent: (function() {
if (this.get('content') != null) {
return fn(this.get('content'));
}
}).property('content')
});
};
App.registerViewHelper('formatNumber', inlineFormatter(function(content) {
return new Handlebars.SafeString("Formated number: " + content);
}));
此链接很有用:来自@Jo Liss的http://techblog.fundinggates.com/blog/2012/08/ember-handlebars-helpers-bound-and-unbound/
答案 1 :(得分:1)
你正在寻找一个尚未存在的绑定帮助器。有一个Pull Request和相关的讨论。
答案 2 :(得分:0)
我认为stackoverflow上存在类似的问题,与绑定帮助器相关检查此链接Writing a helper that produces bound results?