Calling App.store.commit() too fast in Ember

房东的猫 提交于 2019-12-07 14:32:13

问题


In my application I have a list of items, and a button to delete the last one. Then on my controller I wrote the following action:

  removeLastItem: ->
      lastItem = current_order.get('items').get('lastObject')
      lastItem.deleteRecord()
      App.store.commit()

My issue comes when I keep clicking on my button too fast. At some point it seems that while the store.commit() has not finished (the item stills dirty), it is already calling the store.commit() for another item, throwing this error:

Error: Attempted to handle event deleteRecord on App.Item:ember6954:f6a1c932-2db0-4933-7c92-69fbd3838229> while in state rootState.deleted.uncommitted. Called with undefined

I already tried to put this code inside a RunLoop or a Transaction, but nothing worked.

Any clues ? :)


回答1:


You could try a different approach, like for example disabling your button until the record's didDelete event is fired.

Example:

  removeLastItem: ->
    # get the reference to your button and disable it
    lastItem = current_order.get('items').get('lastObject')
    lastItem.deleteRecord()
    lastItem.on 'didDelete', =>
      # reenable your button again

    lastItem.on 'becameError', =>
      # reenable your button again and notify user?

    App.store.commit()

See here for info on the model lifecycle and all the events you can listen to.

Hope it helps.




回答2:


I found another very-simple-approach.

In this blog post they notice that we could just use the ember-data model-cycle flags.

So we could just use this code in our template (suppose the template is user.emblem)

if isSaving
  = will show this if some crud action is performing on the user ercord
else
  = good stuff


来源:https://stackoverflow.com/questions/17218832/calling-app-store-commit-too-fast-in-ember

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!