Pio
Pio

Reputation: 4062

Emberjs Router with requirejs

When building a relatively large application, how should I define my router? More specifically, if using requirejs I have the following code:

main.js

define('application',['routes/app_router'], function(router){
        return Ember.Appcliation.create(
              LOG_TRANSITIONS:true,
              ...
});

requirejs('application',function(application){
      var App = window.App = application;
       ...
}

and in the routes/ I have app_router.js

define('app_router',['ember'],function(){
  ...
});

So should I pass an app to my app_router to set the App.Router.map... method or should I return a Ember.Router.map(...)? If the first variant is chosen then for sure, the dependencies change.

In other words, should I create an "empty" Ember.Application and pass it to the router so it can define the App.Route.map(... method, since it has reference to this, like this.route\this.resource..., or should I invoke Ember.Router.create() then invoke the map function on it, then return this from the module and set it to App.Router = router.

Upvotes: 1

Views: 137

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

So should I pass an app to my app_router to set the App.Router.map... method or should I return a Ember.Router.map(...)? If the first variant is chosen then for sure, the dependencies change.

I'd go with the 2nd variant.

In other words, should I create an "empty" Ember.Application and pass it to the router so it can define the App.Route.map(... method, since it has reference to this, like this.route\this.resource..., or should I invoke Ember.Router.create() then invoke the map function on it, then return this from the module and set it to App.Router = router.

Neither. You should let ember create the router itself. All your code should be doing is calling App.Router's map fx. I'm no require.js expert, but something like this should work:

//Define a fx that specifies your applications routes
define('routes',['ember'], function(){
  return function() {
    this.route("about");
  }
});

// Pass that custom routes fx to App.Router.map before routing begins
define('application',['routes'], function(routes){
  return Ember.Application.create({
  LOG_TRANSITIONS: true,
  ready: function() {
    this.Router.map(routes);
  }
});

Here's a jsfiddle showing the basic concept, without require.js of course.

Upvotes: 1

Related Questions