2014-02-20 32 views
0

我經歷的例子與大衛Sulc的書Backbone.Marionette.js路由:一個溫柔的介紹木偶AppRouter,無法定位到正確的URL

https://leanpub.com/marionette-gentle-introduction

ContactManager.navigate = function (route, options) { 
    options || (options = {}); 
    Backbone.history.navigate(route, options); 

}; 


ContactManager.getCurrentRoute = function() { 
    return Backbone.history.fragment; 
}; 


ContactManager.on("initialize:after", function() { 

    if (Backbone.history) { 
     Backbone.history.start(); 

     if (this.getCurrentRoute() === "") { 
      ContactManager.trigger("contacts:list"); 

     } 

    } 

正如你所看到的如果歷史片段爲空,它將觸發聯繫人列表事件,該事件將呈現聯繫人列表。然而,它根本沒有重定向,並且我發現該片段預設爲「聯繫人」,因此事件根本不會被解僱。它也發生在我身上一次,最初的片段是空的,並得到了一切呈現,並正確地更改url,但刷新片段仍然「聯繫」,並再次沒有呈現。

ContactsApp.Router = Marionette.AppRouter.extend({ 
     AppRoutes: { 
      "contacts": "listContacts" 

     } 
    }); 

ContactManager.on("contacts:list", function() { 
     ContactManager.navigate("contacts"); 
     API.listContacts(); 

    }); 

這是處理事件的代碼。什麼似乎是問題?謝謝。

+0

我的回答有助於解決問題嗎? – bejonbee

+0

感謝您的回答。看起來我在代碼的其他地方出現了一個錯誤,併成功地對其進行了調試。 – Mefhisto1

回答

1

我認爲有一些缺少的代碼。我希望找到這樣的路由器:

var myController = { 
    listContacts: function() { 
     ContactManager.trigger("contacts:list"); 
    } 
}; 

ContactsApp.Router = Marionette.AppRouter.extend({ 
    controller: myController, 
    appRoutes: { 
     "contacts": "listContacts" 
    } 
}); 

注意appRoutes開始以小寫一個。

現在路由contacts會調用控制器的listContacts方法並觸發ContactManager.on("contacts:list"...回調,運行相應的API方法。