如何将一个余烬对象传递给车把助手

时间:2013-08-21 10:08:39

标签: ember.js handlebars.js

我想将一个ember对象传递给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使用script元素返回值。因此,它不能在标签内使用。还有其他任何帮助吗?

//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

提前致谢...

2 个答案:

答案 0 :(得分:0)

我不确定我理解你的问题。您正在将item.label传递给助手,但您希望将该项目作为参数?为什么不做{{act item}}

另外,为什么要尝试模仿Ember中already exists{{#linkTo}}助手?

答案 1 :(得分:0)

请检查以下内容。

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>