2013-08-07 62 views
0

我使用的是ToDo示例中相同的基本路由器代碼,但我遇到了一些問題。當我Router.navigateToItem(itemID)一切正常。但是,如果我輸入直接url(/ inventory/itemId),那麼Session變量永遠不會被設置,因此直接的URL不起作用。我可以弄清楚如何讓路由器通過直接URL觸發。感謝幫助,非常感謝。這裏是我正在使用的代碼:骨幹路由器不會觸發直接URL - 使用流星

var WebRouter = Backbone.Router.extend({ 
    routes: { 
     "": "main", 
     "inventory/:itemId": "itemDetail" 
    }, 
    main: function() { 
     Session.set("inventoryItem", null); 
    }, 
    itemDetail: function(itemId) { 
     Session.set("inventoryItem", Items.find({_id:itemId}).fetch()[0]); 
    }, 
    navigateToItem: function(itemId) { 
     this.navigate("inventory/"+itemId, {trigger: true}); 
    } 
    }); 

    Router = new WebRouter; 

    Meteor.startup(function() { 
    Backbone.history.start({pushState: true}); 
    }); 

編輯1:

我注意到,如果我不pushState的:

Backbone.history.start(); 

然後一切似乎工作。不過,後來我在我的網址這個愚蠢的哈希符號,我不知道它是從哪裏來的:/#庫存/ WsL7YZxiWk3Cv3CgT

越來越近......我也不清楚什麼是不pushState的丟失......

編輯2:

另一個失敗的嘗試:

window.onload= function(){ 
    var url = window.location.pathname; 
    Router.navigate(url.substring(1,url.length), {trigger: true}); 
    console.log(url.substring(1,url.length)); 
    }; 

我真的以爲這一次是去上班,但它不...

+0

這並不能解決你目前的問題,但你有沒有嘗試[流星路由器](https://github.com/tmeasday/meteor-router)?它使用起來非常簡單(IMO),並且可以工作到IE8。 –

+0

即時通訊網絡開發新手。這對我來說看起來不太簡單 - 我不知道page.js是什麼,然後我開始瀏覽並找到越來越多的我不明白的地方......:/ – Chet

+0

是的,你需要隕石來使用它,但是一旦你安裝了隕石,只需要編寫'mrt add router'來將路由器添加到你的項目中即可。它會在這個過程中添加page.js(並且你不需要知道關於page.js的任何內容,它只是依賴於它)。 –

回答

0

我想通了。它碰巧是一個更微妙的問題,很難解釋,我不完全確定我完全理解它:

我會設置一個會話變量從數據庫中的對象,而不是設置會話變量到數據庫對象ID和被動連接該數據庫對象的模板......

無論如何,我改變了這些行:

Template.inventory.inventoryItem = function() { 
    return Session.get("inventoryItem"); 
    } 

    Session.set("inventoryItem", Items.find({_id:Session.get("inventoryItem")}).fetch()[0];); 

更改爲:

Template.inventory.inventoryItem = function() { 
    return Items.find({_id:Session.get("inventoryItem")}).fetch()[0]; 
    } 

    Session.set("inventoryItem", itemId); 

現在一切都神奇地工作...

2

您需要CONFI確定您的服務器重定向所有URL以提供請求根時獲取的內容。在加載頁面後,Backbone只處理URL,如果你沒有配置服務器來處理URL,那麼Backbone永遠不會被加載,因爲HTML永遠不會被髮送。