ember.js - Using set() on an array -
i have emberarray caches activities @ day level. therefore if i've pulled activities server 1st july found in activitiesbyday['2013-07-01']. problem how get() , set() these array items can take advantage of ember's run loop , data binding properties.
here's full code:
module.exports = app.activitiesroute = app.authenticatedroute.extend({ ondate: null, actionlist: ember.arraycontroller.create(), activitiesbydate: ember.array, activitiesondate: function() { return this.get('activitiesbydate')[this.get('ondate')].content; }.property('activitiesbydate','ondate'), if (!this.get('activitiesbydate')[params.on_date]) { this.set('activities', this.store.find('activity', {on_date: params.on_date})); this.get('activitiesbydate')[params.on_date] = this.store.filter('activity',function(activity){ var itemdate = new date(activity.get('start_time')); itemdate = moment(itemdate).format('yyyy-mm-dd'); return itemdate === params.on_date; }); } as can see, line item says ...
this.get('activitiesbydate')[params.on_date] =
is problem because i'm making changes without letting ember know. problem can't find way use ember's set() on array who's keys dates of format "yyyy-mm-dd". key structure, believe, requires me use javascript's bracket notation versus dot notation ember should handle.
---- update ----
i have updated activitiesroute little activitiesbydate object rather ember.array. have changed setters/getters properties of object use embers get/set()`
app.activitiesroute = app.authenticatedroute.extend({ ondate: null, activitiesbydate: ember.object.create(), activitiesondate: function() { return this.get('activitiesbydate.' + this.get('ondate')); }.property('activitiesbydate'), model: function(params) { this.set('ondate', params.on_date); // check if given date has activities loaded , if not load // if not reload. in case of wanting refresh use refreshdate() method if (!this.get('activitiesbydate')[params.on_date]) { console.log('getting activities for: ' + params.on_date); this.set('activities', this.store.find('activity', {on_date: params.on_date})); // go dates server this.set('activitiesbydate.' + [params.on_date], this.store.filter('activity',function(activity){ var itemdate = new date(activity.get('start_time')); itemdate = moment(itemdate).format('yyyy-mm-dd'); return itemdate === params.on_date; })); } return true; }, setupcontroller: function(controller, model) { controller.set('activitiesbydate',this.get('activitiesbydate')); } the activitiesbydate object seems work fine. here's example of it's state looks after having gone 3 separate dates:

looking inside these objects can see contain right amount of activities part looks good. unfortunately computed property -- activitiesondate not being updated outside of when hit reload in browser.
if use set('someproperty.' + dynamicproperty, value) , get('someproperty.' + dynamicproperty), have want. in case:
getting filtered result
//instead of this.get('activitiesbydate')[this.get('ondate')] // use this.get('activitiesbydate.' + this.get('ondate')); setting filtered result
// instead of var filtered = this.store.filter('activity',function(activity){ var itemdate = new date(activity.get('start_time')); itemdate = moment(itemdate).format('yyyy-mm-dd'); return itemdate === params.on_date; }); this.get('activitiesbydate')[params.on_date] = filtered; // use var filtered = this.store.filter('activity',function(activity){ var itemdate = new date(activity.get('start_time')); itemdate = moment(itemdate).format('yyyy-mm-dd'); return itemdate === params.on_date; }); // if key change, notify entire property ember.propertywillchange(this, 'activitiesbydate'); this.set('activitiesbydate.' + params.on_date, filtered); ember.propertydidchange(this, 'activitiesbydate');
Comments
Post a Comment