Ember.js Ember.computed在方法中定义时未按预期运行

Ember.js Ember.computed在方法中定义时未按预期运行,ember.js,Ember.js,我正试图使用Ember.computed在视图的一个方法中设置computed属性。我试图使用这把小提琴中显示的语法,但正如你所看到的,它似乎并没有达到我所希望的效果。任何指向正确方向的指示都将不胜感激 史蒂夫这不会以这种方式工作,因为余烬必须执行它的一些魔法。我查看了灰烬的来源,发现: 因此,在您的情况下,以下内容应该适用: Ember.defineProperty(this, 'myComputed', Ember.computed(function() { return "fun

我正试图使用Ember.computed在视图的一个方法中设置computed属性。我试图使用这把小提琴中显示的语法,但正如你所看到的,它似乎并没有达到我所希望的效果。任何指向正确方向的指示都将不胜感激


史蒂夫

这不会以这种方式工作,因为余烬必须执行它的一些魔法。我查看了灰烬的来源,发现:

因此,在您的情况下,以下内容应该适用:

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

挖得好。这也是我最终确定的答案。那么,如何为计算属性提供一个依赖键,使其保持更新?
// 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.
Ember.defineProperty(this, 'myComputed', Ember.computed(function() {
    return "funky";
}).property());