2014-10-17 33 views
0

我繼承了一些Ember.js代碼,並出於某種原因,開發商沒有在這裏使用Ember.ArrayController的集合,所以我手動Ember.SortableMixin混合。下面是我想與之合作的代碼:排序在Ember.Controller

App.NflProjectionsController = Ember.Controller.extend(Ember.SortableMixin, { 
    position: 'qb', 
    site: 'FanDuel', 

    type: 'nfl_weekly_projection',  
    isLoading: true, 

    collection: [], 

    isQB:  Ember.computed.equal('position', 'qb'), 
    isWR:  Ember.computed.equal('position', 'wr'), 
    isRB:  Ember.computed.equal('position', 'rb'), 
    isTE:  Ember.computed.equal('position', 'te'), 
    isK:  Ember.computed.equal('position', 'k'), 
    isDEF: Ember.computed.equal('position', 'def'), 
    isFlex: Ember.computed.equal('position', 'flex'), 

    isFanDuel:  Ember.computed.equal('site', 'FanDuel'), 
    isDraftKings: Ember.computed.equal('site', 'DraftKings'), 
    isDraftStreet: Ember.computed.equal('site', 'DraftStreet'), 
    isFantasyAces: Ember.computed.equal('site', 'FantasyAces'), 
    isDraftDay: Ember.computed.equal('site', 'DraftDay'), 

    actions: { 
    choosePosition: function(type){ 
     this.set('collection', []); 
     this.set('position', type); 
    }, 

    chooseSite: function(type){ 
     this.set('collection', []); 
     this.set('site', type); 
    }, 

    sortBy: function(property) { 
     this.set('sortProperties', [property]); 
     this.set('sortAscending', !this.get('sortAscending')); 
    } 
    }, 

    positionObserver: function(){ 
    this.set('isLoading', true); 
    var self = this; 
    this.store.find(this.get('type'), {position: self.get('position'), site_id: self.get('site')}).then(function(collection){ 
     // console.log("here we are"); 
     if (self.get('collection').length == 0) 
     self.set('collection', collection.toArray()); 
     self.set('isLoading', false); 
    }); 
    }.observes('position').on('init'), 

    siteObserver: function(){ 
    this.set('isLoading', true); 
    var self = this; 
    this.store.find(this.get('type'), {position: self.get('position'), site_id: self.get('site')}).then(function(collection){ 
     if (self.get('collection').length == 0) 
     self.set('collection', collection.toArray()); 
     self.set('isLoading', false); 
    }); 
    }.observes('site').on('init') 

}); 

我創建了sortBy功能,我試圖找出我怎麼能時設置到控制器的collection屬性數組排序。我見過的所有例子都可以直接使用Ember.ArrayController。是否可以使用此現有代碼和Ember.Controller進行排序?

回答

1

是不是有原因你不要想要只使用ArrayController?如果您collection屬性可以被認爲是該頁面的後盾模型,那麼這將是標準的做法是通過通過底層路線填充model屬性以替換collection財產。

如果你想保持你有現有的頁面,那麼你在正確的軌道上Ember.SortableMixin。本來,我想你需要使用Ember.ArrayProxy採取sortBy優勢,但@ Kingpin2k在灰燼居然直接擴展陣列原型的評論好心人士指出,因此,即使這是沒有必要的。

@ Kingpin2k把一個很好的jsbin顯示這個動作:

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

+0

'sortBy'通過由燼包含數組原型擴展添加,你不需要換東西與Ember。 A或ArrayProxy來使用它們。 http://emberjs.jsbin.com/gepeko/1/edit – Kingpin2k 2014-10-17 04:38:28

+0

@ Kingpin2k謝謝你的澄清!我沒有意識到Ember擴展了js數組的原型。我現在要修改我的答案。 – 2014-10-17 04:42:47