2013-03-11 41 views
5

目前我有灰燼的對象,看起來像這樣:把手傳遞對象爲輔助

name: 'Bob' 
xs: { 
    'actual':50 
    'target':55 
} 

我有類似xs周圍5-6場。我需要一個可以接受這個xs對象的幫助器方法,然後返回目標是否被擊中。

我想這樣做的:

Handlebars.registerHelper('hasHitTarget', function(attribute) { 
    if (attribute.actual >= attribute.target) 
    { 
     return block(this); 
    } 
}); 

{{#each user in App.userController}} 
    {{#hasHitTarget user.xs}} 
     Target Hit 
    {{/hasHitTarget}} 
{{/each}} 

一切我在網上看了說這應該工作。但事實並非如此。當我console.log(attribute)它作爲一個字符串返回user.xs。這是怎麼回事?

回答

2

把手& Ember.Handlebars,Ember在內部擴展了把手以增加額外的功能。

這就是說你在這裏使用錯誤的幫手,你需要使用Ember.Handlebars.registerBoundHelper

Ember.Handlebars.registerBoundHelper('hasHitTarget', function(attribute) { 
    if (attribute.actual >= attribute.target) { 
    return block(this); 
    } 
});