ember.js - ember-data observe record array isLoading/isUpdating/hasUpdated? -
i trying display list of calendar events week. kicker trying reuse data in ds.store. code , part works.
the problem eventschanged
observer running way many times. when router fetches events server eventschanged
observer fires each record. understand why happens. don't why there not way check if recordarray done loading batch of records.
does have suggestions?
app.calendarweekroute = ember.route.extend model: (params) -> ember.object.create date: params.date login: params.login serialize: (model) -> date: model.get 'date' login: model.get 'login' setupcontroller: (controller, model) -> app.event.find login: model.get 'login' week: model.get 'date' events = app.event.all() days = app.daysofweekfor( model.get('date') ) #returns array of dates controller.setproperties content: days events: events app.calendarweekcontroller = em.arraycontroller.extend itemcontroller: 'calendarday' events: null eventschanged: ( -> #don't run if more records scheduled loaded #return if @get 'events.isupdating' days = @get 'content' children = @get '_subcontrollers' events = @get 'events' user = @get 'user' return unless days , children , events , user days.foreach (date) -> controller = children.findproperty 'content', date content = events.filter (event) -> (event.get('date') date) , (event.get('user') user) if content controller.set 'content', content ).observes('content', 'events.@each')
edit current hack. using unofficial ember.debounce
eventschanged: ( -> days = @get 'content' children = @get '_subcontrollers' events = @get 'events' user = @get 'user' return unless days , children , events , user days.foreach (date) -> controller = children.findproperty 'content', date content = events.filter (event) -> (event.get('date') date) , (event.get('user') user) if content controller.set 'content', content ).observes('content', 'didupdate') notifyeventschanged: ember.debounce( @ (_this) -> _this.notifypropertychange('didupdate') 200 false ).observes('events.@each')
ok have solution.
- use
query
instead offind
. - bind listener on
didload
attribute of recordarray , toggle attributeisupdating
on controller - observe controller
content
,events
attributes separately.
app.calendarweekroute = ember.route.extend setupcontroller: (controller, model) -> login = model.get 'login' week = model.get 'week' events = app.event.all() newevents = app.event.query({login: login, week: week}, events) controller.setproperties isupdating: true content: days events: events newevents.on 'didload', -> controller.set('isupdating', false) app.calendarweekcontroller = em.arraycontroller.extend itemcontroller: 'calendarday' content: ( -> @update() ).observes('content') eventschanged: ( -> @update() unless @get 'isupdating' ).observes('events.@each', 'isupdating') update: -> days = @get 'content' children = @get '_subcontrollers' events = @get 'events' user = @get 'user' days.foreach (date) -> controller = children.findproperty 'content', date content = events.filter (event) -> (event.get('date') date) , (event.get('user') user) controller.set 'content', content
Comments
Post a Comment