简体   繁体   English

Ember js在控制器内使用把手帮手?

[英]Ember js use handlebars helper inside a controller?

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}} 我可以使用{{getStatusText 1}}在视图中使用帮助器

But how do I use the helper in an action inside an ObjectController ? 但是如何在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 为了更好地理解,这里是jsbin

With ember-cli it can be done like this: 使用ember-cli可以这样做:

// helpers/foo.js
export function foo(params) {
    return params;
}
export default Ember.Helper.helper(foo);

Helper foo exports a function (containing the helper logic) and the function wrapped in an Ember helper (for use in a template). Helper foo导出一个函数(包含辅助逻辑)和包装在Ember帮助器中的函数(用于模板)。

// helpers/bar.js
import { foo } from '<project>/helpers/foo';
export function bar(params) {
    return foo(params);
}
export default Ember.Helper.helper(bar);

Helper bar imports the helper function from foo and uses it in it's own template helper. Helper barfoo导入helper函数,并在它自己的模板助手中使用它。

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 http://emberjs.jsbin.com/cenep/1/edit

Most helpers are simple. 大多数助手都很简单。 In this case, exporting a vanilla function is the way to go. 在这种情况下, 导出vanilla function可行的方法。 Pass the function exactly the data it needs. 将功能完全传递给所需的数据。

Ember also has class-based helpers for a more complex use case. Ember还为更复杂的用例提供了基于类的帮助程序。 They can leverage other container dependencies. 他们可以利用其他容器依赖项。 You can still have a class-based helper's compute method call your exported vanilla function. 您仍然可以使用基于类的帮助程序的compute方法调用导出的vanilla函数。

However, the parameter list to the function could get unwieldy for other callers. 但是,函数的参数列表可能会对其他调用者变得难以处理。

import Helper from 'ember-helper';
import service from 'ember-service/inject';

export function vanillaFunction(imageService, userService, foo, bar, baz, dependency3, options) {
  ...
}

export default Helper.extend({

  imageService: service('image'),
  userService: service('user'),

  compute(positionalParams, hashParams) {
    return vanillaFunction(this.get('imageService'), this.get('userService'), positionalParams[0], positionalParams[1], ...);
  },
});

To benefit from container lookups, rather than call the vanilla function, you can manually instantiate such a helper and call compute yourself. 要从容器查找中受益,而不是调用vanilla函数,您可以手动实例化这样的帮助程序并自己调用compute This is rare . 很少见 But it benefits from a small interface, uniform with how you'd call it in the template. 但它受益于一个小的界面,与您在模板中调用它的方式一致。 The helper is normally instantiated by HTMLBars for use within a template, which has special context and observation rules. 帮助程序通常由HTMLBars实例化,以便在模板中使用,该模板具有特殊的上下文和观察规则。 So there's a little hoop jumping to use it inside your eg controller. 因此,在你的eg控制器中有一个小箍跳跃使用它。

import Controller from 'ember-controller';
import getOwner from 'ember-owner/get';
import setOwner from 'ember-owner/set';

export default Controller.extend({
  someMethod() {
    const owner = getOwner(this);
    const helperFactory = owner.resolveRegistration('helper:my-helper');
    const helperInstance = helperFactory.create();
    setOwner(helperInstance, owner); // I'm not sure why the factory doesn't do this for you
    return helperInstance.compute([1]); // "Fresh"
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM