1.

What Is Dynamic Routing?

Answer»

In case of Dynamic ROUTING, you can use variables in the route. For example, you might want to retrieve a post with a VARIABLE id with a friendly URL string. You can specify variable name in the route as :variablename in dynamic routing.

<script>
VAR AppRouter = Backbone.Router.extend({
routes: {
"posts/:id": "getPost",
"*actions": "defaultRoute" // Backbone will try to match the route above first
}
});
// Instantiate the router
var app_router = new AppRouter;
app_router.on('route:getPost', function (id) {
// Note the variable in the route definition being passed in here
alert( "Get post number " + id );
});
app_router.on('route:defaultRoute', function (actions) {
alert( actions ); 
});
// Start Backbone history a NECESSARY step for bookmarkable URL's
Backbone.history.start();
</script>

In case of Dynamic Routing, you can use variables in the route. For example, you might want to retrieve a post with a variable id with a friendly URL string. You can specify variable name in the route as :variablename in dynamic routing.

<script>
var AppRouter = Backbone.Router.extend({
routes: {
"posts/:id": "getPost",
"*actions": "defaultRoute" // Backbone will try to match the route above first
}
});
// Instantiate the router
var app_router = new AppRouter;
app_router.on('route:getPost', function (id) {
// Note the variable in the route definition being passed in here
alert( "Get post number " + id );
});
app_router.on('route:defaultRoute', function (actions) {
alert( actions ); 
});
// Start Backbone history a necessary step for bookmarkable URL's
Backbone.history.start();
</script>



Discussion

No Comment Found