Search code examples
meteoriron-router

Meteor: How should I write this Iron Router route?


I'm trying to rewrite my Iron Router adminhome router but when I click on the link to /admin/home I get the error below.

How should I rewrite this route?

Router.route('adminhome', {
      layoutTemplate: 'adminlayout',
      path:'/admin/home',
      template: 'adminarea',
      onBeforeAction: function() {
          if (Meteor.loggingIn()) {
              this.render(this.loadingTemplate);
          } else if(!Roles.userIsInRole(Meteor.user(), ['admin'])) {
              console.log('redirecting');
              this.redirect('/');
          } else {
              this.next();
          }
      }
});

This is the error I get:

Exception from Tracker recompute function:
debug.js:41 Error: Expected template rendered with Blaze.render
    at Object.Blaze.remove (view.js:679)
    at DynamicTemplate.destroy (iron_dynamic-template.js:327)
    at null._render (iron_layout.js:400)
    at doRender (view.js:351)
    at view.js:199
    at Function.Template._withTemplateInstanceFunc (template.js:457)
    at view.js:197
    at Object.Blaze._withCurrentView (view.js:538)
    at viewAutorun (view.js:196)
    at Tracker.Computation._compute (tracker.js:323)

Here's my updated code. The problem might be related to the {{>yield}} line. That's just a guess though.

    Router.route('adminhome', {
      layoutTemplate: 'adminlayout',
      path:'/admin/home',
      template: 'adminarea',

      onBeforeAction: function() {
          if (Meteor.loggingIn()) {
              //this.render(this.loadingTemplate);
              this.render("loadingPage");
          } else if(!Roles.userIsInRole(Meteor.user(), ['admin'])) {
              console.log('redirecting');
              this.redirect('/');
          } else {
              this.next();
          }
      }
});


<template name="adminTemplate">
    {{#if isInRole "admin"}}
        {{> adminarea}}
    {{else}}
        Must be admin to see this...
    {{/if}}
</template>

<template name="adminarea">
   {{>yield}}
</template>

Solution

  • In the line of your code...

    this.render(this.loadingTemplate);

    ...replace this.loadingTemplate with the actual loading template name.

    Eg.

    this.render("LoadingTemplateName");