Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
Rick Avatar asked Nov 29 '12 08:11

Rick


People also ask

What is routing on a website?

Routing is the mechanism by which requests (as specified by a URL and HTTP method) are routed to the code that handles them. As we've already noted, routing used to be file based and very simple: if you put the file foo/about. html on your website, you would access it from the browser with the path /foo/about.

What is Iron router?

Router. A router that works on the server and the browser, designed specifically for Meteor.

What are the benefits of client side routing?

Client-side routing allows you to have unique URLs for each View, but will also make the app work faster—instead of needing to download an entire brand new page from the server, you only need to download the requisite extra data (e.g., using an AJAX request), with much of the other content (the HTML, CSS, etc) already ...

What are routes in react?

React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. Let us create a simple application to React to understand how the React Router works.


2 Answers

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.

like image 53
Andrew Mao Avatar answered Sep 20 '22 17:09

Andrew Mao


Lot of people use this route system:

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

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

like image 29
acemtp Avatar answered Sep 23 '22 17:09

acemtp