0

I am wondering if there is a way to use a library or other custom function within the router.js file (using iron-router) for a meteor app.

The code is like this:

Router.map(function() {
  this.route('editorder', {path: '/editorder/:_id', template:'editorder',
      data: function() { 
        if(this.params._id == 'new') 
          return OrderFactory.newOrder();
        else
          return OrderFactory.getOrder(this.params._id);
      }
  });
}

The only way I can get this to work is to define the OrderFactory above this call to Router.map (ie. within the router.js file). Does meteor have a way to bootstrap some code before it runs any of its own initialization? I would like to use OrderFactory within router.js and also elsewhere.

Thanks for any help!

Update / Solution: The issue was that I was defining the OrderFactory with the 'var' keyword. Changing it to:

OrderFactory = { ... }; 

solved the problem. This is related to specifics about Meteor namespacing, described here: http://docs.meteor.com/#/full/namespacing

Ryan Weiss
  • 1,308
  • 1
  • 14
  • 36
  • All I can think about is trying to change the [file load order](http://docs.meteor.com/#/full/fileloadorder). Rename router.js to main.router.js and check if it works. – user3557327 Nov 21 '14 at 18:16
  • Hrm, tried it but it still says OrderFactory is not defined. – Ryan Weiss Nov 21 '14 at 18:41
  • Is your router.js file inside the /lib directory? files there are loaded before the rest. Try moving it elsewhere. – user3557327 Nov 21 '14 at 18:44
  • It's in the main directory. I only have common.js and router.js in the main directory, and common.js includes the OrderFactory library. I tried renaming router.js to main.router.js also. I have seen that perhaps I can create a package for the Order stuff, and then possibly bootstrap the app with the OrderFactory and router.js files as requirements? But I don't think that's ideal. – Ryan Weiss Nov 21 '14 at 19:05
  • 1
    Are you creating OrderFactory like `var OrderFactory = ...`? In Meteor `var` makes the variable visible only inside it's own file. [Here's the doc about it](http://docs.meteor.com/#/full/namespacing). – user3557327 Nov 21 '14 at 19:13
  • Wow, good guess! That seemed to have fixed it. I had it as: var OrderFactory = {}; Changing it to OrderFactory = {}; solved the problem. Thanks a ton! – Ryan Weiss Nov 21 '14 at 19:26

0 Answers0