jm2
jm2

Reputation: 763

Run function after Backbone.Router.navigate()

I want to run a function (to render flash messages) after a backbone.navigate call. Is it possible? I currently hooked into the all event and check if eventname starts with "route:". But that triggers too early, before my messages are added... so I was thinking of running after the route completes

# intercept all router navigate and render flash messages if any
renderFlashMessengesOnNavigate = (evt) =>
  if evt.indexOf("route:") is 0 
    console.log "rendering messages after route ..."
    @flashMessenger.render()
window.appRouter.bind "all", renderFlashMessengesOnNavigate
window.authRouter.bind "all", renderFlashMessengesOnNavigate
window.userRouter.bind "all", renderFlashMessengesOnNavigate

Upvotes: 1

Views: 1866

Answers (1)

timDunham
timDunham

Reputation: 3318

Try this:

var originalNavigate = Backbone.history.navigate;
Backbone.history.navigate = function(fragment, options){
    originalNavigate.apply(this, arguments);

    //your code here
}

Upvotes: 7

Related Questions