Return route to Backbone js

Like Django {{ url }} , is there a way or way to access a specific route by passing it a name and variables.

 // example Router var router = Backbone.Router.extend({ routes: { '!/user/:user_id': 'editUserAction', '!/': 'homeAction' }, editUserAction(user_id) { // edit user view }, homeAction() { // home view } }); 

Some methods like

 router.reverse('editUserAction', '5'); 

Would return a hash:! !/user/5

 router.reverse('homeAction'); 

Would return a hash:! !/

+8
source share
2 answers

Discussion of reverse routing. https://github.com/documentcloud/backbone/issues/951

simple hack

 var personRoutes = { list: "/persons", show: "/persons/:id", edit: "/persons/:id/edit" } var getRoute = function(obj, route, routeDefinitions){ return routeDefinitions[route].replace(":id", obj.id); } var person = new Model({id: 1}); var route = getRoute(person, "edit", personRoutes); // => "/persons/1/edit" 
+5
source share

Unfortunately no, there is nothing like the built-in trunk. I wanted something like this, and the discussion of this on the list was once or twice - perhaps even a pull request (I don’t remember for sure at the moment). But it is not done yet.

The best I came up with is to write my own methods that create the correct route string:

 function userEditPath(userId){ return "/user/" + userId + "/edit"; } 
+4
source share

All Articles