layout | title | type | posturl |
---|---|---|---|
post |
What is a router? |
beginner |
Backbone routers are used for routing your applications URL’s when using hash tags(#). In the traditional MVC sense they don’t neccesarily fit the semantics and if you have read “What is a view?” it will elaborate on this point. Though a Backbone “router” is still very useful for any application/feature that needs URL routing/history capabilities.
Defined routers should always contain at least one route and a function to map the particular route to. In the example below we are going to define a route that is always called.
Also note that routes intepret anything after “#” tag in the url. All links in your application should target “#/action” or “#action”. (Appending a forward slash after the hashtag looks a bit nicer e.g. http://example.com/#/user/help)
{% highlight html %}
Activate route
Activate another route
{% endhighlight %}
Please note: * Prior to Backbone 0.5 (released 1. July 2011) a Router was called a Controller. To avoid confusion, the Backbone developers changed the name to Router. Hence, if you find yourself using an older version of Backbone you should write Backbone.Controller.extend({ * });
Most conventional frameworks allow you to define routes that contain a mix of static and dynamic route parameters. For example you might want to retrieve a post with a variable id with a friendly URL string. Such that your URL would look like “http://example.com/#/posts/12”. Once this route was activated you would want to access the id given in the URL string. This example is implemented below.
{% highlight html %}
{% endhighlight %}
Backbone uses two styles of variables when implementing routes. First there are “:params” which match any URL components between slashes. Then there are “*splats” which match any number of URL components. Note that due to the nature of a “*splat” it will always be the last variable in your URL as it will match any and all components.
Any “*splats” or “:params” in route definitions are passed as arguments (in respective order) to the associated function. A route defined as “/:route/:action” will pass 2 variables (“route” and “action”) to the callback function. (If this is confusing please post a comment and I will try articulate it better)
Here are some examples of using “:params” and “*splats”
{% highlight javascript %}
routes: { “/posts/:id”: “getPost”, // Example “/download/*path”: “downloadFile”, // Download “/:route/:action”: “loadView”, // Load Route/Action View }, getPost: function( id ){ alert(id); // 121 }, downloadFile: function( path ){ alert(path); // user/images/hey.gif }, loadView: function( route, action ){ alert(route + “_” + action); // dashboard_graph }{% endhighlight %}
Routes are quite powerful and in an ideal world your application should never contain too many. If you need to implement hash tags with SEO in mind, do a google search for “google seo hashbangs”.
Remember to do a pull request for any errors you come across.
No Tips and Tricks
- Herman Schistad (Backbone 0.5 rename from Controller to Router)
- Paul Irish