我试图按周显示日历事件列表。我正在尝试重用DS.Store中已有的数据。这是执行它的代码,并且在大多数情况下它可以工作。
问题是我的eventsChanged
观察者运行的次数太多了。当路由器从服务器获取事件时,eventsChanged
观察者将为每条记录触发。我理解为什么会这样。我没有得到的是为什么没有办法检查recordArray是否完成加载一批记录。
有人有任何建议吗?
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 are scheduled to be loaded
#return if @get 'events.isUpdating'
days = @get 'content'
children = @get '_subControllers'
events = @get 'events'
user = @get 'user'
return unless days and children and events and user
days.forEach (date) ->
controller = children.findProperty 'content', date
content = events.filter (event) ->
(event.get('date') is date) and
(event.get('user') is user)
if content
controller.set 'content', content
).observes('content', 'events.@each')
编辑这是我目前的黑客行为。使用unofficial Ember.debounce
eventsChanged: ( ->
days = @get 'content'
children = @get '_subControllers'
events = @get 'events'
user = @get 'user'
return unless days and children and events and user
days.forEach (date) ->
controller = children.findProperty 'content', date
content = events.filter (event) ->
(event.get('date') is date) and
(event.get('user') is user)
if content
controller.set 'content', content
).observes('content', 'didUpdate')
notifyEventsChanged: Ember.debounce(
@
(_this) ->
_this.notifyPropertyChange('didUpdate')
200
false
).observes('events.@each')
答案 0 :(得分:2)
好的我有一个很好的解决方案。
query
代替find
。didLoad
属性上绑定侦听器,并在控制器上切换属性isUpdating
content
和events
属性。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') is date) and
(event.get('user') is user)
controller.set 'content', content
答案 1 :(得分:0)
您可能希望观察events.isLoaded
并在函数的核心中测试其值。