Reputation: 71
I am facing a problem with navigating/routing the URL. Say for example, I am currently in #contact/new
and again I am requesting the same url then its not calling the respective function. I need to change the URL to for example #contact/edit
and then clicking #contact/new
is working. Routing the URL from the same URL is a problem now?
Upvotes: 4
Views: 2738
Reputation: 9130
I ended up solving this problem by calling a silent navigate to the root in advance:
var url = window.location.pathname.substr(Backbone.history.root.length);
this.navigate('/', { trigger: false });
this.navigate(url, { trigger: true });
Keep in mind that this will create an extra history entry, but in my case, this isn't important.
You might also get this to work without the history entry by changing the fragment
property of the Backbone.history
object, but this property is supposed to be private, so I would think twice before fooling around with it :)
The fragment property is what is used to match the current URL with the URL that is passed in to the navigate function, and thus the route will execute if they don't match.
Upvotes: 2
Reputation: 48
I know I'm late to the party, but I solved a similar issue by using the navigate function.
Try something like:
this.navigate("#contact/edit", {trigger: false, replace: true});
Which changes the URL the router thinks it is on to #contact/edit without actually invoking the action.
Upvotes: 1
Reputation: 11
I've been trying to figure this problem out as well. My latest attempt was to add a function that appends a timestamp field to the link, then calls the navigate function:
var AppRouter = Backbone.Router.extend({...});
var app_router = new AppRouter;
function LoadLink(Link) {
var Timestamp = new Date().getTime();
app_router.navigate("#/" + Link + "/ts_" + Timestamp);
}
This method allows me to have a link that can be clicked multiple times, however now I have to take the timestamp into account when setting up routes. It might come back to bite me later. :(
Upvotes: 1
Reputation: 2399
I have the same issue. Routing the same url example.com/#/new (calling the same url twice) will not trigger the route. I created a workaround by changing the url after each routing:
var url = window.location.href;
url = url.substring(0,url.indexOf('#')) + '#/';
window.location.replace(url);
or easier:
window.location.hash = '#/';
But this solution seems to me a bit dirty.
Upvotes: 1