2016-02-26 22 views
0

我正在嘗試這樣做:我有一個名爲'trip'的模型,並且在旅程中,名爲'createdToday'的屬性返回創建旅行的日期。我想要的是返回今天所做的旅行列表。Ember今天創建的模型的返回長度

這是我此行模式:

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    driver: DS.belongsTo('driver', { 
     async: true, 
     inverse: 'trip' 
    }), 


    ..... etc ....... 


    createdAt: DS.attr('string', { 
     defaultValue() { 
      return new Date(); 
     } 
    }), 
    isBookedToday: function(trip) { 
     var today = new Date().toDateString(); 
     return (today === trip.get('createdAt').toDateString); 
    }, 
    getTripsToday: Ember.computed('[email protected]', function() { 
     var tripsToday = this.get('trip'); 
     return tripsToday.filterBy('isBookedToday', true).get('length'); 
    }) 

}); 

在我isBookedToday,我想看看如果一個個體出行的創建時間是一樣的今天的時間,並在getTripsToday,我通過努力環所有旅行和過濾isBookedToday。

而在我的.hbs文件中,我說:{{trips.getTripsToday}},它不會呈現任何內容,所以出現了錯誤。

我想我最困惑於Ember的@each以及它究竟是如何工作的。

感謝您的任何反饋意見。

回答

1

首先,您必須瞭解您的旅行模型實例代表單人旅行!它絕對不是放置功能的恰當地方,它可以爲您提供經過篩選的旅行列表!

Next isBookedToday是一個普通函數而不是計算屬性。所以你不能filterBy就可以了。

可能要實現在您的旅行isBookedToday,但你一定要過濾的同一個地方您的旅行,你接他們!可能在model()掛鉤或計算屬性componentcontroller

所以,你可以但不需要你models/trip.js做:

... 
isBookedToday: Ember.computed('createdAt', { 
    get() { 
     let now = new Date(); 
     let created = get(this, 'createdAt'); 
     return now.getFullYear() === created.getFullYear() && 
      now.getMonth() === created.getMonth() && 
      now.getDate() === created.getDate(); 
    } 
}) 
... 

,然後在model鉤:

model() { 
    return this.store.findAll('trip').then(trips => trips.filterBy('isBookedToday')); 
} 

或者在計算財產在controller或者component

tripsToday: Ember.computed('[email protected]', { 
    return get(this, 'trips').filterBy('isBookedToday'); 
}) 

要小心。如果您在一夜之間打開頁面,這會導致令人困惑的事情!當你的日期改變時,計算屬性將自動重新計算而不是