2014-05-21 119 views
0

我想排序計算屬性上的相關對象的ArrayController(relevance)。相關性根據您正在查看的主要模型進行計算。EmberJS根據ItemController或全局屬性對ArrayController進行排序?

麻煩似乎是,雖然你可以用控制器來裝飾模型,但是在那裏添加顯示邏輯/屬性時,你不能對這些屬性進行排序。 (參見:Ember ArrayController cannot be sorted by property defined in itemController

我創建了一個JSBin,抓住這個最:http://jsbin.com/guluxaco/1/edit

我知道我可以通過推動相關屬性下降到模型得到這個工作,但不認爲這是適當的要爲模型可達另一個控制器(或全局屬性)

我看到兩種方法:

1)推relevance下到模型(相關對象)。在那種情況下,我應該在哪裏存儲當前模型(主要對象)?

2)獲取ArrayController重新排序?也許排序或裝飾路線中的模型?

回答

0

我看看你的代碼,我想出了一個解決方案,因此,我所做的是改變你的模式是這樣的:

model: function() { 
    return [Ember.Object.create({color: 'red'}), 
     Ember.Object.create({color: 'yellow'}), 
     Ember.Object.create({color: 'blue'})]; 
} 

,然後,你的顏色控制器這樣的:

App.ColorsController = Ember.ArrayController.extend({ 
    needs: ['mood'], 
    sortProperties: ['relevance'], 

    contentChanged: function() { 
    Ember.run.once(this, function() { 
     this.get('content').forEach(function(color) { 
     color.set('relevance', color.get('color') === this.get('controllers.mood.mood') ? 1 : 0); 
     }, this); 
    }); 
    }.observes('[email protected]', 'controllers.mood.mood') 
}); 

希望這會有所幫助,我將您的jsbin更改爲此代碼並且它正在工作。

+0

現在我再次讀你的問題,我認爲這個解決方案是你不想做的,把屬性放在模型中。 – fanta

相關問題