2014-10-17 57 views
0

我們假設我有兩個型號userpost。一個user可以有多個post,每個post屬於一個user。在特定頁面上,我想根據日期顯示post,通常只有今天的帖子。 我應該如何設計關係?如何在燼數據中處理基於時間的關係?

我可以在控制器上創建方法類似

App.ProfileController = Ember.ObjectController.extend 
    getPostsForToday: (-> 
      @store.all('posts').filterProperty('date', '2014-10-17') 

,但在我看來,我應該把這個邏輯分爲模型

App.User = DS.Model.extend 
    todayPosts: (-> 
      DS.hasMany("posts").filterProperty('date', '2014-10-17') //something likes this 

但是這種方式我不能動態地更改日期, 我可以嗎?根據日期處理關係的最佳策略是什麼?

回答

2

我認爲要做的事情將是使用ArrayController,這有內置排序,並過濾結果進入你的路線。

ArrayController:

App.TodaysPostsController = Ember.ArrayController.extend({ 
    sortBy: 'date' 
}); 

控制器:

App.ProfileController = Ember.ObjectController.extend({ 
    needs: ['todaysPosts'], 
    todaysPosts: Ember.computed.alias('controllers.todaysPosts'), 
}); 

路線:

App.ProfileRoute = Ember.Route.extend({ 
    setupController: function(controller, model) { 
     // perform default functions 
     this._super(controller, model); 

     this.store.findAll('post'); 

     var $this = this; 
     var today = '2014-10-17'; 

     // this will updated whenever any of the posts models change 
     this.store.filter('post', { 'date': today }, function(post) { 
      return post.get('date'); 
     }) 
     .then(function(todaysPosts) { 
      $this.controllerFor('todaysPosts').set('model', todaysPosts); 
     }); 
    } 
}); 

信息陣列控制器http://emberjs.com/api/classes/Ember.ArrayController.html

信息在商店濾波01上