Meteor without Router


#1

Hi,

is it a good idea to use meteor without a router?

Work with Template.dynamic and session var…


#2

I’d say the main reason to use a router is if you want pages of your app to be linkable to others. If you don’t need this then it’s not necessary.


#3

IMHO, a “true” single page app doesn’t need a router, but that is a matter of taste.


#4

Ok thanks,

i have a disadvantage when i use a router, when is not needed?


#5

Not really a disadvantage but why have possibly slower performance & extra code in your project that you don’t really need?


#6

ok i think the same.
do you have a good workaround to manage the templates without a router?


#7

I’d do this:

Meteor.startup(function () {
  $("body").on("click", "a", function (event) {
    // Prevent default action
    event.preventDefault();
    // Get page attribute
    var pageName = this.page;
    // Update current route
    Session.set("page", pageName);
  })
) 

// List your templates somewhere
Session.set("templates", {
  pageName: templateName, 
  pageName2: templateName2
});

Template.layout.helpers({
  currentPage: function () {
    // Reactively retrieve template name from page session
    var templates = Session.get("templates");
    var pageName = Session.get("page");

    return templates[pageName];
  }
})

layout.html

{{> Template.dynamic template=currentPage}}

If you don’t mind the app reloading the page when navigating you could simplify this by using pathnames instead of pageName in the Session.set("templates"); and then retrieve the templateName using templates[window.location.pathname] in the currentPage helper - removing the need for the jquery click event.


#8

thank’s for this example, i think i work without Iron Router or Flow Router.

thank’s for help