在Ember中调用App.store.commit()太快 [英] Calling App.store.commit() too fast in Ember

查看:128
本文介绍了在Ember中调用App.store.commit()太快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个项目列表,一个按钮删除最后一个。然后在我的控制器上,我写了以下动作:

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()

当我坚持点击我的按钮太快时,我的问题就来了。在某些时候,似乎
store.commit()尚未完成(该项目仍然脏),它已经在调用另一个项目的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:


错误:尝试在
上处理事件 deleteRecord App.Item:ember6954:f6a1c932-而状态为rootState.deleted.uncommitted的
则为2db0-4933-7c92-69fbd3838229。调用未定义

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

我已经尝试将此代码放在RunLoop或事务中,但没有任何作用。

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

任何线索? :)

推荐答案

您可以尝试其他方法,例如禁用按钮,直到记录的 didDelete 事件被触发。

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

示例:

  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.

希望有帮助。

这篇关于在Ember中调用App.store.commit()太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆