I am separating my views and router into separate files with require. I then have a main.js file that instantiates the router, and also renders my default view.
My router has view ('View/:id') and edit ('Edit/:id') as routes. In main.js, when I instantiate the router, I can hardcode router.navigate('View/1', true) and the navigation works fine. In my view file, when I click on the edit link, I want to call router.navigate('View/' + id, true), but I'm not sure how I should do this.
I've had success calling Backbone.history.navigate('View/' + id, true), but I don't feel like I should be relying on the global Backbone object.
I tried passing ({ router: appRouter }) to my views so I could use this.options.router.navigate(), however that wasn't working for me.
In case you're curious, here's a bunch of code from my app:
Router:
define(['./View', './Edit'], function (View, Edit) {
return Backbone.Router.extend({
routes: {
'View/:id': 'view',
'Edit/:id': 'edit'
},
view: function (id) {
var model = this.collection.get(id);
var view = new View({ model: model });
view.render();
},
edit: function (id) {
var model = this.collection.get(id);
var edit = new Edit({ model: model });
edit.render();
}
});
});
View:
define(function () {
return Backbone.View.extend({
template: Handlebars.compile($('#template').html()),
events: {
'click .edit': 'edit'
},
render: function () {
//Create and insert the cover letter view
$(this.el).html(this.template(this.model.toJSON()));
$('#View').html(this.el);
return this;
},
edit: function () {
Backbone.history.navigate('Edit/' + this.model.id, true);
},
});
});
for me i added an object to the main application like so;
and inside your view, you can say windows.app.router.navigate({'',trigger:true}) . Don't know if global scoping is good practice in this case, but it worked for me.
For me, the solution with goTo function worked with a slight change
I know this question is old, but I am wondering why haven't you use require.js in order to get the router:
As with pretty much any Backbone question, there are lots of ways to handle this. The way I approached it in my current project was to put everything in a global custom namespace, and use that to pass around the necessary references:
Views could then refer to
MyNamespace.router
as necessary. But it looks like this won't work/isn't encouraged with require.js, so here are some other options:Don't ever call the router explicitly - instead, change a global state object that the router listens to. This is actually how I've done things in my current project - see this response for more details.
Attach the router to your top-level view, often called
AppView
, make that globally accessible, and useAppView.router.navigate()
.Make another module that provides a
navigate
utility function that callsBackbone.history.navigate()
internally. This isn't much different from what you're doing, but it would make it slightly more modular and keep you from using the global reference all the time. This also allows you to change the internal implementation.You could do it the old fashioned way with window.location.hash :)
Here's an alternate solution if you don't need explicit routes. When you app starts up create an object that extends Backbone Events
Then anywhere in you app you can listen for events
And also from anywhere dispatch the event,
data
below are any params you want to send along for the rideIn case anyone else is looking for a solution to this problem like I was, I'm posting what I ended up doing. If you're using the boilerplate backbone.js, then you will have an
initialize()
function inrouter.js
. I modified myinitialize()
function to look like the following:Due to backbone.js's particular flavour of inheritance, this allows allows me to call
MyView.goTo(location);
from within any of my views.