backbone.js - multiple matching routes -


i've got backbone.js application defines 2 controllers, , controllers both define route patterns match location.hash. i'm having trouble getting both of them fire - e.g.

managercontroller = backbone.controller.extend({    routes: {       ":name":      "dostuff"    },     dostuff : function(name) {       console.log("dostuff called...");    } });  component1controller = backbone.controller.extend({    routes: {       "xyz123":      "domorestuff"    },     domorestuff : function() {       console.log("domorestuff called...");    } }); 

so if url "http://mysite.com/#xyz123", seeing 'dostuff()' called, or if comment out route, 'domorestuff()' called. not both.

i'm using architecture because page highly component oriented, , each component defines own controller. 'component manager' defines controller house keeping on routes.

should able configure 2 controllers both respond same route? cheers,

colin

short answer: no, can't that. 1 controller per page.

long answer: when instantiate new controller, adds routes history singleton. history singleton monitoring hash component of url, , when hash changes, scans routes first expression matches needs. fires function associated route (that function has been bound controller in declared). fire once, , if there conflict order in fires formally indeterminate. (in practice it's deterministic.)

philosophical answer: controller "view" object affects presentation of whole page based on hash component of url. purpose provide bookmark-capable urls user can reach in future, when goes url can start pre-selected view among many. description, sounds you're manipulating publicly exposed, manually addressable item manipulate different parts of viewport, while leaving others alone. that's not how works.

one of nice things backbone if pass route that's regular expression, use as-is. if you're trying use controller create bookmarkable description of layout (component 1 in upper right hand corner in display mode "a", component 2 in upper left corner in display mode "b", etc) can suggest number of alternatives-- allocate each 1 namespace in hash part of url, , create routes ignore rest, i.e.

routes: {     new regexp('^([^\/]*)/.*$'): 'docomponent1stuff',     new regexp('^[^\/]*/([^\/]*)\/.*$': 'docomponent2stuff', } 

see how first uses items after first slash, second after second slash, etc. can encode magic entirely how want.

i suggest, though, if you're going doing , feel of components, , want reasonably persistent, views getting , setting cookies local store; if they're small enough, cookies enough.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -