Ember js在控制器内部使用handlebars帮助器? [英] Ember js use handlebars helper inside a controller?

查看:73
本文介绍了Ember js在控制器内部使用handlebars帮助器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Ember.Handlebars.helper('getStatusText'函数(value,options){
switch(value){
case 1:returnFresh;
break;
case 2:returnCallback;
break;
default:return无法获取状态;
}
});

我可以使用 {{getStatusText但是,如何在ObjectController中的一个动作中使用助手?

  Test.DealController = Ember.ObjectController.extend({

selectedStatusType:null,
statusList:[Fresh,Callback ],

actions:{
updateStatus:function(deal){
//如何在这里调用handlebars帮助?
console.log({{getStatusText 1}});
}
},

});

这显然不起作用。



其他方式是什么?



为了更好的理解,这里是 jsbin

解决方案把这个逻辑从助手中拉出来,使它可以由助手和非句柄帮助器一起调用。将其解析为手柄模板并对其进行评估正在使其复杂化。



您所在的位置取决于您,您可以将其命名为应用程序,或仅将其创建为一个与你的帮手一起生活的功能。

  function getStatusText(value){
switch(value){
case 1:returnFresh ;
break;
case 2:returnCallback;
break;
default:return无法获取状态;
}
}

Ember.Handlebars.helper('getStatusText',function(value,options){
return getStatusText(value);
} );

http://emberjs.jsbin.com/cenep/1/edit


I have a helper method that maps a number to a text -

Ember.Handlebars.helper('getStatusText', function (value, options) {
    switch(value) {
        case 1: return "Fresh";
            break;
        case 2: return "Callback";
            break;
        default: return "Unable to get Status";
    }
});

I am able to use the helper in the view by using {{getStatusText 1}}

But how do I use the helper in an action inside an ObjectController ?

Test.DealController = Ember.ObjectController.extend({

    selectedStatusType: null,
    statusList: ["Fresh","Callback"],

    actions: {
        updateStatus: function(deal) {
// How do I call the handlebars helper here ?
            console.log({{getStatusText 1}});
        }
    },

});

this obviously does not work.

What are the other ways ??

For better understanding, here is the jsbin

解决方案

Pull the logic out of the helper, making it available to be called both by the helper, and by non handlebars helper items alike. Parsing it into handlebars template and evaluating it is over complicating it.

Where you put it is up to you, you could namespace it to your app, or just create it as a function that lives with your helper.

function getStatusText(value){
    switch(value) {
        case 1: return "Fresh";
            break;
        case 2: return "Callback";
            break;
        default: return "Unable to get Status";
    }
}

Ember.Handlebars.helper('getStatusText', function (value, options) {
  return getStatusText(value);
});

http://emberjs.jsbin.com/cenep/1/edit

这篇关于Ember js在控制器内部使用handlebars帮助器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆