2014-05-22 26 views
0

我試圖所以這個,但它不工作指定在.property)動態字符串(

App.FacetCheckboxView = Ember.Checkbox.extend({ 
    facetName: null, 
    facetValue: null, 
    checked: function() { 
     var s = this.get('facetValue'); 
     var selected = this.get('controller.' + this.get('facetName')); 

     return selected.contains(s); 

    }.property('controller.'+ this.get('facetName') +'[email protected]') 

}) 

如果我在它的工作性質方法參數利用一個靜態字符串來代替。有沒有其他方法可以實現這一目標?

回答

1

當然,你可以在初始化

App.FacetCheckboxView = Ember.Checkbox.extend({ 
    init: function(){ 
    this._super(); 
    Ember.defineProperty(this, 'checked', Ember.computed(function() { 
     var facetValue = this.get('facetValue'), 
      options = this.get('controller.' + this.get('facetName')); 
     return options.contains(s); 
    }).property("controller." + this.get('facetName') + ".[]", 'facetValue')); 
    }, 
    facetName: null, 
    facetValue: null 

}); 

定義計算的屬性我做了一個例子,我不能完全肯定,如果這是你如何使用視圖,但它應該讓你在正確的道路。

http://emberjs.jsbin.com/vopewayo/1/edit

個人現在看來似乎會更容易只是沒有定義的值作爲一個字符串,但作爲收藏,但我不知道您的具體使用情況。

+0

謝謝,它的工作。 – ryudice