ember.js - Ember return length of a model created today -
i trying this: have model called 'trip', , inside trip, attribute called 'createdtoday', returns date when trip created. want return list of trips made today.
here trip model:
import ds '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('trip.@each.createdat', function() { var tripstoday = this.get('trip'); return tripstoday.filterby('isbookedtoday', true).get('length'); }) });
in isbookedtoday, i'm trying see if individual trip's created time same todays time, , in gettripstoday, trying loop through trips , filtering isbookedtoday.
and in .hbs file, i'm saying: {{trips.gettripstoday}}, won't render anything, something's wrong.
i guess confused @ ember's @each , how works.
thanks feedback.
first have understand trip model instances represents single trip! absolutely not right place put function gives filtered list of trips!
next isbookedtoday
normal function not computed property. can't filterby
on it.
you may want implement isbookedtoday
on trip, have filter trips on same place fetch them! in model()
hook or computed property on component
or controller
.
so could don't need in models/trip.js
:
... isbookedtoday: ember.computed('createdat', { get() { let = new date(); let created = get(this, 'createdat'); return now.getfullyear() === created.getfullyear() && now.getmonth() === created.getmonth() && now.getdate() === created.getdate(); } }) ...
and in model
hook:
model() { return this.store.findall('trip').then(trips => trips.filterby('isbookedtoday')); }
or in computed property in controller
or component
:
tripstoday: ember.computed('trips.@each.isbookedtoday', { return get(this, 'trips').filterby('isbookedtoday'); })
be careful. result in confusing things if leave page open overnight! when date changes computed properties not recompute automatically!
Comments
Post a Comment