2012-02-15 43 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' 
    #...