Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 如何将余烬对象传递给把手辅助对象_Ember.js_Handlebars.js - Fatal编程技术网

Ember.js 如何将余烬对象传递给把手辅助对象

Ember.js 如何将余烬对象传递给把手辅助对象,ember.js,handlebars.js,Ember.js,Handlebars.js,我想将余烬对象传递给Handlebar辅助对象,我使用了辅助对象,如下所示,但它没有接收到正确的对象。它只是在控制台中打印对象的名称,请参见下面的代码 //ember helper Ember.Handlebars.registerHelper('act', function (value) { console.log(value); //It prints as item.label return value+"1"

我想将余烬对象传递给Handlebar辅助对象,我使用了辅助对象,如下所示,但它没有接收到正确的对象。它只是在控制台中打印对象的名称,请参见下面的代码

//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",                
        }),
//余烬助手
余烬.把手.注册表帮助器('act',功能(值){
console.log(value);//它作为item.label打印
返回值+1;
}); 
//模板
{{{#内容中的每个项目}
  • {{/每个}} //我正在使用的余烬模型 App.AllRoutes=[Ember.Object.create({ 标签:“索引”, 链接:“#/”, }), Ember.Object.create({ 标签:“第二”, 链接:“#/秒”, }),
    更新 尽管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.handlebar.registerBoundHelper('act',函数(值){
    console.log(value);//它作为item.label打印
    如果(value==“somevalue”){返回新的handlebar.SafeString(“class='active');}
    }); 
    //模板
    {{{#内容中的每个项目}
    
    {{/每个}}
    
    我正在使用 余烬版本:1.0.0-rc.7, 把手。版本:1.0.0


    提前感谢…

    我不确定我是否理解您的问题。您正在将
    项.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

    
    {{outlet}}
    {{{#控制器中的每个项目}
    
  • {{/每个}}
    “item”是一个对象,它包含链接和标签属性,这就是我传递item.label属性的原因。请参阅帮助程序,它返回收到的属性并添加一些额外的字符串。即使我尝试传递item对象,也会遇到同样的问题。但是如果传递了“this”属性,控制台不会打印任何内容,但不会显示错误。我只想添加活动类根据呈现的路由添加到列表项。在{{#linkTo}中,无法绑定路由,因此我正在模拟它。您可以使用
    #linkTo
    添加一个活动类,您只需使用其中两个即可。@srinivasan感谢它的工作。但请查看我更新的问题
    <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>