1

The Trucks model is backed by a db, and accessible via a rest api. The Analysis model, however, is NOT backed by a db but is simply used for computations on the client side.

I need to access the Trucks array from within the Analyses controller, but I encounter 2 problems

  1. Trucks data needs to be retrieved from the database. If I visit the /analyses route immediately then there are no Trucks in the data store when checking the ember console. However, if I visit /trucks first then I notice 6 records are retrieved and in the data store.

  2. Inside the AnalysesController I have specified the dependency on Trucks but I am unable to access the data from within the controller. http://emberjs.com/guides/controllers/dependencies-between-controllers/ shows how to access from within a template.

Below is the code

// router.js
Classmove.Router.map(function() {
  this.resource('trucks');
  return this.resource('analyses');
});

Classmove.TrucksRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('truck');
  }
});

Classmove.Analyses = Ember.Route.extend({
  model: function() {
    return this.store.find('analysis');
  }
});



// analyses_controller.js
Classmove.AnalysesController = Ember.ArrayController.extend({
  needs: ['trucks'],
  trucks: Ember.computed.alias('controllers.trucks'),
  isCalculating: false,
  checkTrucks: function() {
    // I want to access the trucks here
    // I found that I was able to access it like so
    this.get('trucks').model.content 
    // but shouldn't there be a direct way without bypassing Ember
  },
  actions: {
    calculate: function() {
      // does stuff - removed from example for simplicity
    }
  }
});
omarshammas
  • 631
  • 4
  • 18
  • You should use get method to access properties. Instead of `this.get('trucks').model.content` use `this.get('trucks.model')` and do not forget about `observes('trucks.model)` on `checkTrucks` method – Mateusz Nowak Nov 10 '14 at 17:03

1 Answers1

0

I found this answer to be helpful EmberJS: How to load multiple models on the same route?

The router waits for promises to complete on the beforeModel, model and afterModel hooks.

In my example, the afterModel hook returns a hash of promises. When the promises are fulfilled it set the trucks and locations attributes on the AnalyzeRoute. Once the promises have completed, setupController hook is called where we read the attribute from the AnalyzeRoute and set them on the controller.

I'm not reading the data from another controller. I'm reading the data from the store because the TrucksController may not have been visited before the /analyze route is called hence it's model attribute may not have been set.

// router.js.coffee
App.AnalyzeRoute = Ember.Route.extend
  model: -> @store.all('location-analysis') # don't fetch from server, checks local store
  afterModel: ->
    Ember.RSVP.hash
      trucks: @store.find('truck').then (trucks) => @set('trucks', trucks)
      locations: @store.find('location').then (locations) => @set('locations', locations)
  setupController: (controller, model) ->
    this._super(controller, model)
    controller.set 'trucks', @get('trucks')
    controller.set 'locations', @get('locations')



// analyze_controller.js
App.AnalyzeController = Ember.ArrayController.extend
  checkTrucks: ->
    trucks = @get('trucks') # returns an Ember Record Array
    # do some checking
  actions:
    calculate: ->
      // does stuff - removed from example for simplicity
Community
  • 1
  • 1
omarshammas
  • 631
  • 4
  • 18