Basic functions vs computed properties


#1

Any tips or advice on when it is appropriate to use straight javascript functions vs computed properties inside a controller?

Beside calling syntax differences, are there any other important considerations to take into account?

I presume you lose out on some of the observer functionality. And implicitly calls to your function are synchronous so any advantages of the ember run loop are lost. Access to function is not possible in handlebars template unless it is a property?

What about custom promises in the controller?

I am trying to figure out it is OK to use the first approach of these two

App.ApplicationController = Ember.Controller.extend({
  myProperty: "some prop",

  myMethod: function () {
    var variable = "foo";
    return variable;
 }     
});

vs

App.ApplicationController = Ember.Controller.extend({
  myProperty: "some prop",

  myMethod: function () {
    var variable = "foo";
    return variable;
 }.property()     
});

Obviously the calling syntaxes are different.

elsewhere in a route…

basic function:

this.controller.myMethod();

vs property

this.controller.get('myMethod');

Are there cases where it doesn’t make sense to use a computed property?


#2

In general, I’d say you shouldn’t use CPs when it’s not a property :slight_smile:

If you’re working with methods that behave like properties, i.e. they have no side-effects and return a value, use CPs. On the other hand, if you’re doing something that has a side-effect, then it definitely shouldn’t be a CP.