Aborting navigation with Meteor iron-router

前端 未结 5 1268
遥遥无期
遥遥无期 2021-01-12 08:54

I have a (client-side) router in a Meteor app, and links using the {{pathFor}} helper.

I am setting a dirty flag in the Session

5条回答
  •  孤独总比滥情好
    2021-01-12 09:39

    I found that rediecting in stop works, and works even when you aren't changing routes via Router.go (such as by links in my application).

    Here is a coffeescript implementation using a class inherited from RouteController

    class MyRouteController extends RouteController
      stop: ->
        # Save whether you data/form is dirty or whatever state you have in 
        # a Session variable.
        if Session.get('formIsDirty') 
          if !confirm('You have unsaved data. Are you sure you want to leave?')
            # Redirecting to the current route stops the current navigation.
            # Although, it does rerun the route, so it isn't a perfect solution.
            Router.go '/my_route'
            # Return here so we don't perform any more of the stop operation.
            return
        # Otherwise do as normal.
        super
    

提交回复
热议问题