您可以使用下面的 repo 来指导您如何进行嵌入式关联。虽然他们没有使用 mongodb,但重要的是在 ember-data 方面,他们正在做嵌入式关联。
https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js
请注意,此处 App.PhoneNumber 嵌入在 App.Contact 中。但它应该让您了解如何解决您的问题。
App.Contact = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string'),
notes: DS.attr('string'),
phoneNumbers: DS.hasMany('App.PhoneNumber'),
});
App.PhoneNumber = DS.Model.extend({
number: DS.attr('string'),
contact: DS.belongsTo('App.Contact')
});
https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/store.js
App.Adapter = DS.RESTAdapter.extend({
bulkCommit: false
});
App.Adapter.map('App.Contact', {
phoneNumbers: {embedded: 'always'}
});
App.Store = DS.Store.extend({
revision: 12,
adapter: App.Adapter.create()
});
https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js
App.ContactEditController = Em.ObjectController.extend({
needs: ['contact'],
startEditing: function() {
// add the contact and its associated phone numbers to a local transaction
var contact = this.get('content');
var transaction = contact.get('store').transaction();
transaction.add(contact);
contact.get('phoneNumbers').forEach(function(phoneNumber) {
transaction.add(phoneNumber);
});
this.transaction = transaction;
},
save: function() {
this.transaction.commit();
},
addPhoneNumber: function() {
this.get('content.phoneNumbers').createRecord();
},
});