EmberJS: reject an upcoming transition

∥☆過路亽.° 提交于 2019-12-13 22:31:44

问题


I have an emberjs app, with a form to edit an object.
This page contains a "Save" button, which persists the data into the database.

If the object is dirty and the user is going to be transitioned to an other path in the app, I need to warn them about that, and cancel the transition if they reject the confirmation.

App.ObjectEditRoute = Ember.Route.extend
  exit: ->
    if @get('model.isDirty')
      if confirm('All your changes will be lost')
        # We just keep rolling the chain of events and do the transition
      else
        # We want to cancel the transition

There doesn't seem to be any way to cancel the transition from the exit method though.


回答1:


Since the router facelift, this is pretty easy to do using the willTransition event.

App.ObjectEditRoute = Ember.Route.extend
  events:
    willTransition ->
      if @get('model.isDirty')
        if confirm('All your changes will be lost')
          transition.perform()
        else
          transition.abort()


来源:https://stackoverflow.com/questions/18104374/emberjs-reject-an-upcoming-transition

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