mardi 13 septembre 2016

ember relationship as promises

I have two related models:

//models/school.js
import DS from 'ember-data';
import { belongsTo } from 'ember-data/relationships';
import { hasMany } from 'ember-data/relationships';

export default DS.Model.extend({
    name: DS.attr('string'),
    zipCode: DS.attr('string'),

    kids: hasMany('kid',{inverse: 'schoolId',async:true}),
    district: belongsTo('district',{inverse:'schools',async:true}),
    grades: hasMany('grade',{inverse: 'schools'}),  
});

//models/district.js
import DS from 'ember-data';
import { hasMany } from 'ember-data/relationships';

export default DS.Model.extend({

      name: DS.attr('string'),
      zipCode: DS.attr('string'),
      schools: hasMany('school',{inverse: 'district',async:true}),
});

Accordind to the documentation http://ift.tt/2cUFYDe , when I try to get the district by the school, sometimes the district returns null. Like the school werent related to any district, but all schools are related to the district. I don't understand this.

//routes/kid-info.js

import Ember from 'ember';

export default Ember.Route.extend({

setupController(controller,model){ //the model is kid
    this._super(controller,model);
    controller.set('districts',this.store.findAll('district'));
    controller.set('schools',this.store.findAll('school'));
    controller.set('grades',this.store.findAll('grade'));
    var schoolId=model.get('schoolId.id');
    controller.set('selectedSchool',model.get('schoolId'));
    let school=this.store.peekRecord('school',schoolId);
    if(school){
        school.get('district').then((district)=>{
            controller.set('selectedDistrict',district); //the district is sometimes null
        });
      }

},




Aucun commentaire:

Enregistrer un commentaire