[英]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.