2012-08-24 14 views
2

在我使用JavaScript的本地sort()方法做我在灰燼排序的時刻,但在現實中,我知道我應該使用Ember的自己的排序,但不幸的是我完全不知道如何使用它。有沒有關於如何使用SortableMixin的內容豐富的文章?

我不想浪費大家的時間來爲我解釋它(除非你是一個受虐狂),但如果有人知道如何在EmberJS中對一系列模型進行排序的神話般的文章,那麼它會是不勝感激!

目前我有類似以下,其中我只是對實施再次使用本機排序排序,但我停止自己那裏,因爲我想學習的正確方法。

SortOption: Ember.View.extend(
{ 
    template: Ember.Handlebars.compile('<li><a href="javascript:void(0);">{{ option }}</a></li>'), 
    option:  null, 

    click: function() 
    { 
     var rootView = this.nearestWithProperty('people'); 
     var sortBy  = this.get('option'); 

     var sorted = rootView.get('people').orderBy('formalName'); 
     rootView.set('people', sorted) 
    } 
}) 
+0

我試過'orderBy'方法從看'SortableMixin',但螢火告訴我,有沒有這樣的方法。 – Wildhoney

+0

也許我的答案在這裏http://stackoverflow.com/questions/11352646/ember-js-collectionview-order/11353305#11353305可以幫助你。 –

回答

6

This pull request用於更新有關SortableMixin的文檔。

Ember.SortableMixin provides a standard interface for array proxies 
to specify a sort order and maintain this sorting when objects are added, 
removed, or updated without changing the implicit order of their underlying 
content array: 
    songs = [ 
    {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'}, 
    {trackNumber: 2, title: 'Back in the U.S.S.R.'}, 
    {trackNumber: 3, title: 'Glass Onion'}, 
    ]; 

    songsController = Ember.ArrayController.create({ 
    content: songs, 
    sortProperties: ['trackNumber'] 

}); 

songsController.get('firstObject'); // {trackNumber: 2, title: 'Back in the U.S.S.R.'} 
songsController.addObject({trackNumber: 1, title: 'Dear Prudence'}); 
songsController.get('firstObject'); // {trackNumber: 1, title: 'Dear Prudence'} 

Sortable Mixin現在包含在從0.9.8版本開始的ArrayController中(我相信)。您需要做的就是在控制器上定義一個sortProperties數組,並在視圖中使用arrangedContent屬性。 Here is an explanation from Ivan.