boop
boop

Reputation: 7788

Call an action without any interaction

Is it possible to call an controller action without interaction within a template?

I want to convert a number to an specific string like this:

switch (s){
    case 1: return "foo";
    case 2: return "bar";
    // ...
}

I want to do this in an each loop => each over items from my model, convert property s from each item to a specific string.

Well I probably could do this in an afterModel hook - but I would like to do it from the template.

Upvotes: 0

Views: 61

Answers (2)

Oren Hizkiya
Oren Hizkiya

Reputation: 4434

What you need is a bound helper. That can have your logic to convert from the string to whatever string you want to display.

See Register Bound Helper in the docs.

Ember.Handlebars.registerBoundHelper('convertToMyString', function(s) {
    switch (s){
        case 1: return "foo";
        case 2: return "bar";
        // ...
    }
});

And in your template:

{{convertToMyStringname model.s}}

Upvotes: 1

givanse
givanse

Reputation: 14943

Don't, you are working against the framework and all you'll find is hardship.

Do your data processing (decoration/formatting) in your controller and from there make your data available to your template.

Templates are presentation only, no logic should be performed there (as less as possible actually).

App.IndexController = Ember.Controller.extend({
  processedData: function() {
    return // processed data;
  }.property('model')
});

Upvotes: 0

Related Questions