0
votes

I've got a node.js (server) and backbone.js (client) app - I can load and init my backbone app on a page... and init the router, but my default route (".*") is not getting called. I can manually call the index function after I initialize the router, but I don't have to take that step when I've built backbone apps over rails.

Does anyone have a clue as to why this is happening?

adding code (in coffeescript):

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '.*'        : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @registry_patients = new NodeNetBackbone.Collections.RegistryPatients()
    # TODO: Figure out why this isn't sticking...
    @registry_patients.model = NodeNetBackbone.Models.RegistryPatient
    # TODO: Try to only round trip once on initial load
    # @registry_patients.reset($('#container_data').attr('data'))
    @registry_patients.fetch()

    # TODO: SSI - why are the routes not getting processed?
    this.index()

  index: ->
    console.log 'made it to the route index'
    view = new NodeNetBackbone.Views.RegistryPatients.Index(collection: @registry_patients)
    # $('#container').html('<h1>Patients V3: (Backbone):</h1>')
    $('#container').html(view.render().el)
1
Can you show some examples of how you are defining your routes? - loganfsmyth
without code examples we can't see what could be fixed so please provide your code - Sander
Well, I'm just gonna go on a hunch, but, the default route is not '*.'. It's only '' (an empty string). - Nemanja Miljković

1 Answers

0
votes

Backbone routes are not regexes (unless you manually add a regex route using route). From the fine manual:

Routes can contain parameter parts, :param, which match a single URL component between slashes; and splat parts *splat, which can match any number of URL components.

[...] A route of "file/*path" will match #file/nested/folder/file.txt, passing "nested/folder/file.txt" to the action.

And if we check the source, we'll see this:

// Backbone.Router
// -------------------
//...
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var namedParam    = /:\w+/g;
var splatParam    = /\*\w+/g;

So your '.*' route should only match a literal '.*' rather than matching "anything" as you're expecting.

I think you want something more like this:

routes:
  ''          : 'index'
  #...
  '*path'     : 'index'

Make sure your *path route is at the bottom of your route list:

// Bind all defined routes to `Backbone.history`. We have to reverse the
// order of the routes here to support behavior where the most general
// routes can be defined at the bottom of the route map.

This assumption about the "order" of elements in an object seems rather dangerous and ill-considered to me as there is no guaranteed order:

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

I think you'd be better off adding your default *path route manually in your initialize method:

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @route '*path', 'index'
    #...