在自定义句柄助手中访问Ember.Mixin [英] Accessing an Ember.Mixin within a custom handlebars helper
问题描述
在我的例子中,我有一个mixin, App.Format
其中包含一些不同的格式化数字,日期等等的方法,我想通过自定义帮助者直接从模板访问这些方法(例如 {{formatNumber 450324}}
)。我不是直接将方法实现直接在帮助器而不是mixin的原因是我还需要这些方法在控制器,视图等中可用。为了保持我的代码DRY,我绝对不想在mixin和helpers中有重复的代码。
是否有任何规范的emberish方式要完成这个,或者我只是完全错误的想法呢?
编辑:这是一个jsbin来更好地说明问题:
http://emberjs.jsbin.com/wunug/1/edit?html,js,output (看第33-37行)
方法 Mixin.apply 使得工作。
Ember.Handlebars.helper('formatNumber',function(num,decimals){
var format = App.Format.apply({});
return format.formatNumber(num,decimals || 2);
});
Is there an elegant way of using methods defined in an Ember.Mixin object within a custom handlebars helper?
For example, in my case I have a mixin, App.Format
which contains a bunch of different methods for formatting numbers, dates, ect and I would like to make some of these methods accessible directly from templates via custom helpers (e.g. {{formatNumber 450324}}
). The reason I don't just put the method implementation directly in the helper instead of the mixin is that I also need these methods to be available in controllers, views, ect. And in the interest of keeping my code DRY, I definitely do not want to have duplicate code in the mixin and the helpers.
Is there any canonical, "emberish" way to accomplish this, or am I just thinking about it in the completely wrong way altogether?
Edit: Here is a jsbin to better illustrate the problem:
http://emberjs.jsbin.com/wunug/1/edit?html,js,output (look at lines 33-37)
The method Mixin.apply makes the job.
Ember.Handlebars.helper('formatNumber', function(num, decimals) {
var format = App.Format.apply({});
return format.formatNumber(num, decimals || 2);
});
这篇关于在自定义句柄助手中访问Ember.Mixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!