简体   繁体   中英

Routing in Meteor

In Meteor I'm using Backbone in order to provide routing for the different pages in my app. I currently have a profile and an administration page. When I go to the profile page it shows up just as it should, however when I go to administration Meteor falls back to the main page.

As a side-note, if anyone has a better pattern or best-practice for pages in Meteor feel free to share as this is quite cumbersome.

I use the following template to decide what page to show:

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

The pageIs method is as follows:

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

And the following code in my Backbone router:

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);
    }
});

The log statement in the pageIs method will log undefined a couple of times and then log the correct page, even on administration, however Meteor doesn't seem to reload the selected page anyway and the template still hits the last else-statement.

UPDATE : Iron router is being deprecated in favor of Flow Router. There are strong indications that Flow Router will be supported as part of core Meteor in the future.

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

OUTDATED : The previously commonly used router was Iron Router:

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

At its release, Iron Router combined the efforts of the authors of the two most widely used meteor routers ( meteor-router and mini-pages ), and was the de facto offcial router for Meteor prior to Flow Router.

Lot of people use this route system:

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

It's really easy to use and made for Meteor.

In the early days of Meteor the recommendation was to use Backbone for routing.

The router Andrew pointed out in his post, has become the most popular choice: https://github.com/iron-meteor/iron-router

A more minimalistic solution is flow router: https://github.com/meteorhacks/flow-router

To make an informed decsion which one to use, you can read about the differences of both routers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM