11

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.

Gopesh
  • 3,882
  • 11
  • 37
  • 52
Roy M J
  • 6,926
  • 7
  • 51
  • 78

1 Answers1

23

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);
});
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
  • okay, i got this sometime back, but i was thinking of any built-in way with the backbone history or so to detect. I guess ill have to do it this way manually – Roy M J Sep 11 '13 at 09:05
  • 1
    ill leave this question open for 2-3 days more just to know whether there are any other ways through which someone else got this to work..Thanks @Vitaliy Petrychuk – Roy M J Sep 11 '13 at 09:25
  • @VitaliyPetrychuk I kind of like this approach but where am I supposed to run this snippet? In a View? In the Router? What do you suggest? – darksoulsong Dec 19 '13 at 09:34
  • @darksoulsong, I would suggest to use Controller for this snippet. Backbone doesn't have it, but you can create it or if you use Marionette - it's already created for you. If you do not want to create a new Backbone.Controller you can run this snippet in the Router – Vitalii Petrychuk Dec 19 '13 at 10:15