| Ember's examples on their site are always the simplest use-case. For example, find me an example on their site of saving a complex relationship that was newly created by your application. Let's take the example of an invoice. Let's say you have an Invoice model, and an InvoiceLineItem model in a one-to-many relationship. How do I save the creation of a new invoice (with line items) as a single transaction? As far as I can tell, you can't without breaking from Ember Data's very strict 'this is the way things should be done' model. You have to create a new Invoice, then create the InvoiceLineItems doing something like this: var self = this;
self.get('store').createRecord('invoice', {
.. attributes ..
}).save().then(function (invoice) {
return Ember.RSVP.all(self.get('lineItems').map(function (item) {
return self.get('store').createRecord('invoiceLineItem', {
invoice: invoice,
.. attributes ..
}).save();
});
}).then(function (invoiceLineItems) {
// do something
}).then(null, function (error) {
console.error(error);
});
Note that this isn't even taking into account how to rollback INSERTs if, for instance a single invoiceLineItem fails to be created.The 'other' way to do it is to create just an Invoice model, and have a lineItems attribute of type 'raw' and just create a RawTransform that's just as pass-through of whatever the JSON has for that attribute. It works, but at the same time, feels like it's going against the Ember Data Way, especially if you have a need for an InvoiceLineItem to be its own model (i.e. to be able to maintain relationships to other things and look one up by ID via the API). Then you might have something like: - Invoice and InvoiceLineItem models - A RawTransform: App.RawTransform = DS.Transform.extend({
deserialize: function (data) { return data; },
serialize: function (data) { return data; },
});
- An attribute like so (on Invoice model): lineItems: DS.attr('raw'),
- Code that looks like this: var self = this;
self.get('store').find('invoice', invoiceId).then(function (invoice) {
self.get('store').pushPayload('invoiceLineItem', { invoiceLineItems: invoice.get('lineItems') });
});
|
Drupal for instance has a /really/ great project that maintains a bunch of thorough examples (https://drupal.org/project/examples) which were indispensable when I did Drupal a while back.