Get previous router/url in backbone application

Roy M J picture Roy M J · Sep 11, 2013 · Viewed 12k times · Source

I have a backbone application and i would require to know the router from which the current route is accessed. Is it possible?

For eg :-

I reach #/current from #/test1 and also in another instance, from #/test1.

So can i know through some way to detect the previous router hit?

Ive used :

Backbone.history.fragment

and this only gives me the current route accessed and not from which route.

Answer

Vitalii Petrychuk picture Vitalii Petrychuk · Sep 11, 2013

You can store your history in some array and do some manipulation with last/previous items.

var history = [];
this.listenTo(this, 'route', function (name, args) {
  history.push({
    name : name,
    args : args,
    fragment : Backbone.history.fragment
  });
  console.log(history);
});