ember.js – ember-data观察记录数组isLoading / isUpdating / hasUpdated?

我试图按周显示日历事件列表.我正在尝试重用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')

最佳答案 好的我有一个很好的解决方案.

>使用查询而不是查找.
>在recordArray的didLoad属性上绑定侦听器,并在控制器上切换属性isUpdating
>分别观察控制器内容和事件属性.

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
点赞