4

我正在尝试使用 Ember.computed 从我的视图方法之一中设置计算属性。我尝试使用这个小提琴中显示的语法,但正如你所看到的,它似乎并没有真正做到我希望的那样。任何指向正确方向的指针都将不胜感激。

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

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

史蒂夫

4

1 回答 1

5

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

// 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());
于 2013-03-14T11:22:00.893 回答