javascript - Strict parameter matching in backbone.js router -
i've got router defined such:
var myrouter = backbone.router.extend({ routes: { // catch-all undefined routes "*notfound" : "notfound", }, initialize: function(options) { this.route("my_resource/clear_filters", 'clearfilters'); this.route("my_resource/:id", 'show'); }, show: function(id){ console.log('show', id); }, clearfilters: function() { console.log('clearfilters'); }, notfound: function() { console.log('notfound'); }, }); var app = {}; app.myrouter = new myrouter(); backbone.history.start({silent: true});
thus following urls map as:
var opts = {trigger: true}; app.myrouter.navigate('/foo', opts); // logged -> 'notfound' app.myrouter.navigate('/my_resource/123', opts); // logged -> 'show', '123' app.myrouter.navigate('/my_resource/clear_filters', opts); // logged -> 'clearfilters' app.myrouter.navigate('/my_resource/some_thing', opts); // logged -> 'show', 'some_thing'
how can restrict my_resource/:id
route match on numeric parameters app.myrouter.navigate('/my_resource/some_thing')
handled notfound
?
from fine manual:
route
router.route(route, name, [callback])
manually create route router,
route
argument may routing string or regular expression. each matching capture route or regular expression passed argument callback.
so can things like:
this.route(/my_resource\/(\d+)/, 'show')
in router's initialize
if need finer grained control on routes backbone's string patterns give you.
Comments
Post a Comment