Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Routes in a Backbone.js controller?

I want to set a default route for my backbone.js controller. Currently I do it like so:

class DealSearchController extends Backbone.Controller     routes:         'list' : 'showListView'         'photos' : 'showPhotoView'         'map' : 'showMapView'      initialize: ->         ....                     window.location.hash = 'list' if ! _.include( _.keys(@routes),(window.location.hash || '').replace('#','')) 

Is there a better way of doing it?

like image 896
Martin Wawrusch Avatar asked May 22 '11 12:05

Martin Wawrusch


People also ask

How can default route be generated?

Default routes can also be generated by dynamic routing protocols, such as OSPF and IS-IS. Default routes are used only when no matching routing entry is available for packet forwarding in the routing table. A default route in the routing table is the route to the network 0.0.

How Backbone js works?

BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications. When a model changes, it automatically updates the HTML of your application. BackboneJS is a simple library that helps in separating business and user interface logic.

What is module in Backbone JS?

js respectively. The rest of your application code should be divided into modules that can live under their own modules directory. A module is an encapsulated group of structures (for the purposes of our post, Backbone structures) that work cohesively to provide a subset of functionality in your application.

What is Backbone used for?

Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore. js, plus jQuery for use of the full library. It is designed for developing single-page web applications, and for keeping various parts of web applications (e.g. multiple clients and the server) synchronized.


2 Answers

Try adding this additional route as the last route in your controller:

'*path':  'defaultRoute' 

and then handle it like this:

defaultRoute: function(path) {     this.showListView(); } 

This assumes the list route is your preferred default. This should work since Backbone.js will match the routes in order, but will always match the 'splat' route.

like image 104
Bill Eisenhauer Avatar answered Nov 14 '22 01:11

Bill Eisenhauer


You may use the splat route format to define a catch-all route, such as:

routes:   'list' : 'showListView'   '*path': 'defaultRoute'  defaultRoute: ->   ... 

These splats can match any number of URL components. Since the one given here essentially matches anything, the order in which the routes are defined matters. Earlier rules listed in the routes literal take precedence over later ones. So the catch-all rule should be listed last.

A note of warning: The mechanics of the for in statement leave the iteration order of keys in objects unspecified (ECMA-262 section 12.6.4):

The mechanics and order of enumerating the properties ... is not specified.

Most browsers, if not all with some buggy exceptions, will iterate in order of definition. If the routes defined have ambiguity whose correct resolution rely on ordering (like in this case), and/or if an explicit ordering may be preferable because of an unpredictable environment, it is also possible to define routes dynamically in the Router's initializer, rather than declaratively/statically:

initialize: function () {     //router.route(route, name, [callback]);     this.route('*path', 'default', this.defaultRoute);     this.route('map', 'map', this.showMapView);     this.route('photos', 'photos', this.showPhotoView);     this.route('list', 'list', this.showListView); } 

In this case, routes defined later override previously-defined routes, so the order from earlier is reversed to preserve the same behaviour.

like image 43
init_js Avatar answered Nov 14 '22 01:11

init_js