Search code examples
ember.jsember-dataemberfire

How to specify async belongsTo/hasMany relationships in emberfire/emder-data?


I am very new at Ember/ED/EmberFire so apologies if this is a trivial question. I am able to save records to Firebase but I am unable to specify relationships to those records in my controller. I am using

DEBUG: Ember      : 1.10.0
DEBUG: Ember Data : 1.0.0-beta.12
DEBUG: Firebase   : 2.2.3
DEBUG: EmberFire  : 1.4.3
DEBUG: jQuery     : 1.11.2

I have a model as such:

var Patient = DS.Model.extend({
    lastName: DS.attr('string'),
    firstName: DS.attr('string'),
    encounters: DS.hasMany('encounter', {async: true})
});

var Encounter = DS.Model.extend({
    dateOfEncounter: DS.attr('string'),
    patient: DS.belongsTo('patient', {async: true})
});

I am simply trying to specify the patient that my newly created encounter object is associated with. This is my controller:

actions: {
        registerEncounter: function() {
            var newEncounter = this.store.createRecord('encounter', {
            dateOfEncounter: this.get('dateOfEncounter'),
            patient: this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_')         
            });

        newEncounter.save();

        this.setProperties({
            dateOfEncounter: ''
        });
    }
}

I can successfully create the encounter record in Firebase, but there is no associated patient property. All I get is

https://github.com/firebase/emberfire/issues/232


Solution

  • this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_') returns a promise.

    Try this:

    this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_').then(function(pat) {
         var newEncounter = this.store.createRecord('encounter', {
            dateOfEncounter: this.get('dateOfEncounter'),
            patient: pat       
         });
    
         newEncounter.save();
    });