2013-07-19 56 views
2

是否有添加和删除计算属性在运行时侦听的属性的方法?Ember.js - 更改计算属性在运行时侦听的内容

例如

fullName: function(key, value) { 
    //some code here 
}.property('firstName', 'lastName') 

我想删除“lastName”并在运行时添加“soupCan”。这可能吗?

编辑

进一步的信息:“soupCan”在运行时产生的,我不能创建一个属性依赖于它的二/三我不知道那是什么字符串是要提前做的时间。我没有空间或时间来解释这种设计模式,但这是我们似乎需要的边缘案例。

第二个编辑

貌似这已经在github https://github.com/emberjs/ember.js/issues/1128

回答

0

这是我解决问题的方法,但我会高兴地接受任何想出更优雅解决方案的人。 赦免我的咖啡标

### 
    Add the property with the dynamic dependency key as a mixin after creating 
    the dependency key in the context of the object 
### 
init: -> 
    @_super() 
    mix = Ember.Mixin.create({ 
     _fullName: (-> 
      @get('key_from_runtime') #some logic on this key goes here 
     ).property(key_from_runtime) 
    }) 
    mix.apply(@) 
    @set('full_name_applied', true) 

### Keep track of if the mixin has been applied ### 
full_name_applied: false 

### Depend on the shadow property that has the dynamic dependency key ### 
fullName: (-> 
    if _.isNull(@get('_fullName')) 
     '' #predefined blank value 
    else 
     @get('_fullName') 
).property('_fullName', 'full_name_applied') 
3

此信息存储在内部变量_dependentKeys之前解决。你可能想要像removeDependentKeys。但是,这是在即时函数内部定义的,因此您将无法调用它。

如果您愿意,您可以复制逻辑,但可能是考虑替代方案的好主意。

例如,您可以定义一个不同的计算属性fullNameSoup,该属性取决于firstNamesoupCan,而不是在这些属性之间调用代码切换。或者将这个开关逻辑封装到另一个计算属性中!

+0

+1为研究和链接到来源。我还没有准备好接受,B/C我没有解决方案。虽然谢谢! – wmarbut

+1

不用担心。我很想找到能够回答这个问题的人!因为那样他们也可以回答我的[问题](http://discuss.emberjs.com/t/how-does-the-ember-meta-object-system-work/1792)。 :) –