如何在不使用Ember.Helper
ember-cli
我有一组我想要迁移的帮助程序,但我们的应用程序尚未准备好迁移到ember-cli。
解: 使用注册表。
Ember.onLoad('Ember.Application', function(Application) {
Application.initializer({
name: 'registerMyHelper',
initialize: function(registry/*, application*/) {
registry.register('helper:my-helper', MyHelper);
}
});
});
答案 0 :(得分:0)
http://guides.emberjs.com/v1.10.0/templates/writing-helpers/ - 将进一步详细解释:
几种方式:
简单且更复杂的助手示例:
//Helper to translate a number to words:
Ember.Handlebars.registerHelper("skillLevelToWords", function (levelNum) {
if (levelNum < 3) return "Beginner";
if (levelNum == 3 || Ember.isEmpty(levelNum)) return "Intermediate";
if (levelNum > 3) return "Expert";
})
/**
* Helper to itearte object properties and to print each one with value and key.
*/
Ember.Handlebars.registerHelper('eachProperty', function(context, options) {
var ret = "";
var newContext = Ember.get(this, context);
for(var prop in newContext)
{
if (newContext.hasOwnProperty(prop)) {
ret = ret + options.fn({property:prop,value:newContext[prop]});
}
}
return ret;
});