Skip to content

Using Ember loaders

MajorBreakfast edited this page Sep 17, 2013 · 5 revisions

There's another way to require or load files besides calling import.

Let's take a look at the ember-app-kit example:

routes.js

function Routes() {
  this.route('login');
  this.resource('orders', function() {
    this.resource('order', { path: '/:order_id' });
  });
  this.route('logout');
}

Let's define some routes and controllers:

--app
|--controllers
|----order.js
|----orders.js
|--models
|----model.js
|--routes
|----order.js
|----orders.js

You have several ways of requiring objects in your Ember app:

  • call this.controllerFor in setupController hook, like so:
var OrderRoute = Ember.Route.extend({
  setupController: function() {
    // this.controllerFor('orders'); // gives back an instance of OrdersController
  }
});
export default OrderRoute;

Note, that you should use controllerFor only in your router.

  • add needs property to your controller:
var OrderController = Ember.ObjectController.extend({
  needs: ['orders'],
  init: function() {
    this._super();
    // this.get('controllers.orders'); // gives back an instance of OrdersController
  }
});
export default OrderController;
  • use container to look instance of ember object up:
var OrderRoute = Ember.Route.extend({
  setupController: function() {
    // this.container.lookup('route:order'); // gives back an instance of OrderRoute
    // this.container.lookup('controller:order'); // gives back an instance of OrderController
    // this.container.lookup('template:order'); // gives back the compiled handlebars template for order
  }
});
export default OrderRoute;
  • use container to look up an ember object:
var OrderRoute = Ember.Route.extend({
  setupController: function() {
    // this.container.lookupFactory('route:order'); // gives OrderRoute type back
    // this.container.lookupFactory('controller:order'); // gives you OrderController type back
  },
  model: function() {
    // this.container.lookupFactory('model:order') // gives you Order type back
  }
});
export default OrderRoute;

Justa


Clone this wiki locally