2013-08-07 61 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永远不会被发送。