Search code examples
javascriptmeteoriron-router

Move onBeforeAction hook in Meteor iron-router to different file


I use the following function as a helper for my onBeforeAction hook:

var gameFilter = function () {
    this.subscribe('singleGame', this.params.slug);
    var game = this.data();
    if (game) {
        if (game.multiplayer) {
            this.redirect('multiPlayerPage', {slug: this.params.slug});
        } else {
            this.subscribe('singlePlayerPage', this.params.slug);
        }
    } else {
        this.render(this.notFoundTemplate);
    }
    this.next();
};

I use it like this in my route:

onBeforeAction: [gameFilter, playerFilter]

Now this works fantastic. However, I want to move all filters to a different file. So I created a new file in my lib directory and put the following code in:

gameFilter = function () {
    this.subscribe('singleGame', this.params.slug);
    var game = this.data();
    if (game) {
        if (game.multiplayer) {
            this.redirect('multiPlayerPage', {slug: this.params.slug});
        } else {
            this.subscribe('singlePlayerPage', this.params.slug);
        }
    } else {
        this.render(this.notFoundTemplate);
    }
    this.next();
};

The problem is, that I get a ReferenceError, saying that gameFilter is not defined. I think this issue is caused by Meteor's file load order. Is it possible to fix this?


Solution

  • I had a similar issue with Meteor. The Workaround was to change the name of the File or folder to number so that it is loaded first. it my case it was a file called media.js and i changed to 1_media.js and it worked