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

北城以北 提交于 2019-12-20 07:22:22

问题


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


回答1:


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();
});


来源:https://stackoverflow.com/questions/29355416/how-to-specify-async-belongsto-hasmany-relationships-in-emberfire-emder-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!