mardi 9 février 2016

Ember Data Loading the Parent information on Child

I'm trying to make a select box in my 'contracts' template. It lists over a object listing of 'sched'. Scheds however requires a computer value of 'name' to fetch some string from its parent 'schedt'. In my contracts route, in setupController, I load both the model of contracts and then also assign a array of scheds. When I list the scheds however in my select box, it doesn't fetch the parent information as needed to form the computed name value. It does however load information for some of the scheds, from previous schedts loaded in the template.

I have tried doing an store.find on scheds beforehand in setupController. It does load the schedts in to the store (according to the inspector), but it doesn't reflect on the template view.

Here's a screenshot of what happens.

enter image description here

Here is an example of my code.

models/sched.js

import DS from 'ember-data';
import Ember from 'ember';

export default DS.Model.extend({
  term_months: DS.attr('number'),
  term_miles: DS.attr('number'),

  sched_types_id: DS.belongsTo('schedt', {async:true}),

  name: function(){
      return this.get("term_months") + " mo / "
        + this.get("term_miles") + " mi / "
        + this.get("sched_types_id").get("sched_type");
  }.property("term_months", "term_miles", "sched_types_id.sched_type")
});

models/schedt.js

import DS from 'ember-data';

export default DS.Model.extend({
    sched_type: DS.attr('string')
});

routes/contracts/index.js

import Ember from 'ember';

export default Ember.Route.extend({
    setupController(controller, model) {
        controller.set('content', model);

        controller.set('schedules', this.store.find('sched'));
    }
});

templates/contracts/index.hbs

{{#each contract in controller}}
    <tr>
      <td>
          {{#if contract.dealers_sched_id}}
            {{contract.dealers_sched_id.name}}
          {{else}}
            {{view "select" content=schedules optionValuePath="content.id" optionLabelPath="content.name" value=contract.dealers_sched_id prompt="Select Term"}}
          {{/if}}
      </td>
    </tr>
{{/each}}




Aucun commentaire:

Enregistrer un commentaire