jalal
jalal

Reputation: 499

Iron router RouteController gives a not defined error

This works fine:

Router.route('/', function () {
    this.render('home');
});

Router.route('/about', function () {
    this.render('aboutView');
});

However, this gives an error (RouteController 'HomeController' not defined):

var HomeController, MainController;
HomeController = RouteController.extend({
    action: function() {
        this.render('home', {
     });
   }
});

MainController = RouteController.extend({
   action: function () {
     this.render('aboutView', {
     });
   }
});
Router.route('/', {name: 'home', controller: 'HomeController'});
Router.route('/about', {name: 'about', controller: 'MainController'});

I've tried various permutations (taken from the IronRouter documentation) but there is still the same error. What am I doing wrong?

Upvotes: 1

Views: 395

Answers (1)

saimeunt
saimeunt

Reputation: 22696

Either remove this line :

var HomeController, MainController;

To make these global variables.

Or remove the quotes when specifying controllers :

Router.route('/', {name: 'home', controller: HomeController});
Router.route('/about', {name: 'about', controller: MainController});

Why is that so ?

When you define controllers with iron:router you can use 2 different syntax, using a variable identifier of a string.

If you use a variable identifier, the controller has to be declared as a local (file-scoped) variable using the var keyword.

However if you use a string, the controller will get looked as a global variable (a property of the window object), and global variables in Meteor are defined without the var keyword.

Usually you will define your controllers in different files (that's the point of controllers, externalizing routing logic) so it is more common to use the string syntax.

Upvotes: 1

Related Questions