[英]How to pass an ember object to handlebars helper
我想將余燼對象傳遞給車把幫助器,如下所示,我使用了該幫助器,但它沒有收到正確的對象。 它只是在控制台中打印對象的名稱,請參見下面的代碼
//ember helper
Ember.Handlebars.registerHelper('act', function (value) {
console.log(value); //It prints as item.label
return value+"1";
});
//template
{{#each item in content}}
<li ><a {{bindAttr href="item.link"}}>{{act item.label}}</a></li>
{{/each}}
// Ember model which i am using
App.AllRoutes = [Ember.Object.create({
label: "index",
link:"#/",
}),
Ember.Object.create({
label: "second",
link: "#/second",
}),
即使srinivasan解決方案有效, 更新也無法解決以下所示問題。 registerBoundHelper返回帶有腳本元素的值。 因此,它不能在標記內使用。 還有其他幫助嗎?
//ember helper
Ember.Handlebars.registerBoundHelper('act', function (value) {
console.log(value); //It prints as item.label
if (value == "somevalue"){ return new Handlebars.SafeString("class='active'"); }
});
//template
{{#each item in content}}
<li {{act item.label}} ><a {{bindAttr href="item.link"}}>{{label}}</a></li>
{{/each}}
我正在使用Ember.VERSION:1.0.0-rc.7,Handlebars.VERSION:1.0.0
提前致謝...
我不確定我是否理解您的問題。 您正在將item.label
傳遞到幫助器中,但是您希望將該項作為參數? 為什么不只是{{act item}}
呢?
另外,為什么要嘗試模擬Ember中已經存在的{{#linkTo}}
幫助程序?
請檢查以下內容。
JAVASCRIPT:
App=Em.Application.create({});
App.Router.map(function(){
this.route('all');
this.route('index');
this.route('second');
});
Ember.Handlebars.registerBoundHelper('act', function (value) {
return value+"1";
});
App.IndexRoute=Em.Route.extend({
redirect:function(){this.transitionTo('all');}
});
App.AllRoute =Em.Route.extend({
model:function(){
return [Ember.Object.create({
label: "index",
link:"#/"
}),
Ember.Object.create({
label: "second",
link: "#/second"
})];
}
});
的HTML
<script type="text/x-handlebars" >
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="all">
{{#each item in controller}}
<li ><a {{bindAttr href="item.link"}}>{{act item.label}}</a></li>
{{/each}}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.