2012-02-15 39 views
0

我有一个node.js(服务器)和backbone.js(客户端)应用程序 - 我可以加载和启动我的骨干网页应用程序...并初始化路由器,但我的默认路由( “。*”)没有被调用。我可以在初始化路由器后手动调用索引函数,但当我通过rails构建骨干应用程序时,我不必采取这一步骤。未处理的路线

有没有人有线索为什么发生这种情况?

添加代码(在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) 
+2

你能告诉你如何定义你的路由一些例子吗? – loganfsmyth 2012-02-15 22:26:10

+0

没有代码的例子,我们看不到有什么可以修复的,所以请提供你的代码 – Sander 2012-02-16 10:04:18

+0

嗯,我只是想预测一下,但是,默认路由不是'*。''。它只是''''(空字符串)。 – 2012-02-16 13:40:50

回答

0

骨干路由不可正则表达式(除非手动添加使用route一个正则表达式的路线)。从fine manual

路由可以包含参数份,:param,匹配斜线之间的单个URL分量;和splat部分*splat,它可以匹配任意数量的URL组件。

[012] "file/*path"的路线将匹配#file/nested/folder/file.txt,将"nested/folder/file.txt"传递给该操作。

如果我们检查源,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; 

所以你'.*'路线应该只匹配一个'.*',而不是你希望匹配“任何东西”。

我想你想要更多的东西是这样的:

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

确保您*path路线是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. 

这有关对象中的元素的“秩序”的假设似乎相当对我来说是危险和不适当的there is no guaranteed order

未指定枚举属性的机制和顺序(第一个算法中的步骤6.a,第二个步骤中的步骤7.a)。

我想你会更好,在你的initialize方法手动添加默认*path路线:

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' 
    #...