gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:57:09 25 4
gpt4 key购买 nike

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

最佳答案

好的,我有一个很好的解决方案。

  • 使用 query而不是 find .
  • didLoad 上绑定(bind)一个监听器recordArray 的属性并切换属性 isUpdating在 Controller 上
  • 观察 Controller contentevents属性分开。

  • 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

    关于ember.js - ember-data 观察记录数组 isLoading/isUpdating/hasUpdated?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346751/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com