流星路由

时间:2012-11-29 08:56:24

标签: javascript meteor

在Meteor中我使用Backbone来为我的应用程序中的不同页面提供路由。我目前有个人资料和管理页面。当我进入个人资料页面时,它会显示出来,但是当我去管理时,Meteor会回到主页面。

作为旁注,如果有人对Meteor中的页面有更好的模式或最佳实践,请随意分享,因为这非常麻烦。

我使用以下模板来决定要显示的页面:

<template name="root">
    {{> navbar}}
    {{#if pageIs "profile"}}
      {{> profile}}
      {{else}}{{#if pageIs "administration"}}
        {{> administration}}
      {{else}}
        {{> main_page}}
      {{/if}}
    {{/if}}
</template>

pageIs方法如下:

Template.root.pageIs = function(page){
    console.log(Session.get('page'));
    return page === Session.get('page');
}

我的Backbone路由器中的以下代码:

var ProtonRouter = Backbone.Router.extend({
    routes: {
        "profile": "profile",
        "admin": "administration",
        "administration":"administration"
    },
    profile: function () {
        Session.set('page','profile');
    },
    administration: function (){
        Session.set('page', 'administraion');
    },
    mainPage: function(){
        Session.set('page',null);
    }
});

pageIs方法中的日志语句将多次记录未定义,然后记录正确的页面,即使在管理时也是如此,但Meteor似乎无法重新加载所选页面,模板仍然会触及最后一个else语句

3 个答案:

答案 0 :(得分:25)

更新:Iron路由器已被弃用,转而使用Flow Router。将来会strong indications that Flow Router will be supported as part of core Meteor

  

https://github.com/meteorhacks/flow-router

OUTDATED :以前常用的路由器是Iron Router:

  

https://github.com/EventedMind/iron-router

在其发布时,Iron Router结合了两个最广泛使用的流星路由器(meteor-routermini-pages)的作者的努力,并且在流路由器之前是Meteor的事实上的官方路由器

答案 1 :(得分:9)

很多人使用这种路线系统:

https://github.com/tmeasday/meteor-router

这对Meteor来说非常简单易用。

答案 2 :(得分:1)

在Meteor的早期,建议使用Backbone进行路由。

路由器安德鲁在他的帖子中指出,已成为最受欢迎的选择:https://github.com/iron-meteor/iron-router

更简约的解决方案是流路由器:https://github.com/meteorhacks/flow-router

要明确决定使用哪一个,您可以read about the differences两个路由器。