0
我正在嘗試爲Handlebars/Ember應用程序創建一個自定義的「if_eq」助手。該幫助器使用以下語法調用:{{#if_eq item.name'bob'}},但接收2個字符串值進行比較(字面意思是'item.name'和'bob'),而不是「item .name「在它被調用的上下文中。想知道我可能會做錯什麼。謝謝!扶手助手沒有獲得變量值
相關的代碼段列在下面,並且還創建了一個jsbin來說明問題here
助手代碼
Handlebars.registerHelper('if_eq', function(a, b, opts) {
console.log("Comparing ", a, b);
if(a == b)
return opts.fn(this);
else
return opts.inverse(this);
});
模板代碼
<ul>
{{#each item in model}}
{{#if_eq item.name 'bob'}}
<li>We have a bob here!</option>
{{else}}
<li>A non-bob</li>
{{/if_eq}}
{{/each}}
</ul>
嘿科裏,感謝您的答覆。失望地聽到助手不能使用綁定的屬性......幾乎使他們無用。可以使用未綁定的屬性嗎?認爲我有些誤導的手把助手庫,如:https://github.com/danharper/Handlebars-Helpers,但他們遭受同樣的問題,似乎 – MackelRow 2015-03-31 17:39:45
當我說「不玩好, 「我的意思是你可以做到這一點,它只是需要跳過籃球。如果你真的有心做它:http://emberjs.com/api/classes/Ember.Handlebars.html#toc_example-with-multiple-bound-properties – 2015-04-01 08:34:51