Ember.computed does not work as expected if it is defined in a method

I am trying to use Ember.computed to set a computed property from one of my browsing methods. I tried using the syntax shown on this fiddle, but as you can see, it doesn't seem to do what I was hoping for. Any pointers in the right direction would be greatly appreciated.

http://jsfiddle.net/skane/H5ma5/1/

this.set('myComputed', Ember.computed(function() {return "funky"}).property()); 

Steve

+4
source share
1 answer

This will not work this way, since Amber has to do some of his magic. I looked at the source of Ember and found this :

 // define a computed property Ember.defineProperty(contact, 'fullName', Ember.computed(function() { return this.firstName+' '+this.lastName; }).property('firstName', 'lastName')); @method defineProperty @for Ember @param {Object} obj the object to define this property on. This may be a prototype. @param {String} keyName the name of the property @param {Ember.Descriptor} [desc] an instance of `Ember.Descriptor` (typically a computed property) or an ES5 descriptor. You must provide this or `data` but not both. @param {anything} [data] something other than a descriptor, that will become the explicit value of this property. 

So, in your case, the following should work:

 Ember.defineProperty(this, 'myComputed', Ember.computed(function() { return "funky"; }).property()); 
+5
source

Source: https://habr.com/ru/post/1469125/


All Articles