let's have many medicines can prescribed many patients. our model this:
app.medicine = ds.model.extend({ name: ds.attr(), patients: ds.hasmany('user', { async: true }), }); app.user = ds.model.extend({ name: ds.attr(), medicines: ds.hasmany('medicine', { async: true }) }); in scenario, how save records firebase store?
app = ember.application.create(); app.applicationadapter = ds.firebaseadapter.extend({ firebase: new firebase('https://your_firebase.firebaseio.com/') }); app.router.map(function(){ }); app.medicine = ds.model.extend({ name: ds.attr(), patients: ds.hasmany('user', { async: true }), }); app.user = ds.model.extend({ name: ds.attr(), medicines: ds.hasmany('medicine', { async: true }) }); app.indexroute = ember.route.extend({ model: function() { var medicines = this.store.find('medicine'); var users = this.store.find('user'); return { medicines: medicines, users: users }; }, actions: { savepost: function(){ var store = this.store; var medicine1 = store.createrecord('medicine', {name: 'aspirin'}); var patient1 = store.createrecord('user', {name: 'jane'}); var patient2 = store.createrecord('user', {name: 'peter'}); medicine1.save() .then(function(){ return ember.rsvp.promise.all([ patient1.save(), patient2.save() ]) .then(function(){ var promises = []; var patientsofmedicine1 = medicine1.get('patients'); var medicinesofpatient1 = patient1.get('medicines'); var medicinesofpatient2 = patient2.get('medicines'); promises.push(patientsofmedicine1, medicinesofpatient1, medicinesofpatient2); return ember.rsvp.promise.all(promises); }) .then(function(arrayofattachedarrays){ var promises = []; var patientsofmedicine1 = arrayofattachedarrays[0]; var medicinesofpatient1 = arrayofattachedarrays[1]; var medicinesofpatient2 = arrayofattachedarrays[2]; patientsofmedicine1.addobjects(patient1, patient2); medicinesofpatient1.addobject(medicine1); medicinesofpatient2.addobject(medicine1); promises.addobjects(medicine1.save(),patient1.save(),patient2.save()); return ember.rsvp.promise.all(promises); }); }); } } }); notes:
- thanks david govea showing me how works.
- if there's better way this, please post below.
Comments
Post a Comment