I have a data model that has a many to many relship. Let's say that I have a Producer
model, and a Customer
module. A customer can buy from any number of producers, and a produces can serve any number of customers.
In the backend, I have the standard relational configuration for this with three tables:
- customer
- producer
- customer2producer
My Ember models currently look like this:
// models/producer.js
import DS from 'ember-data';
export default DS.Model.extend({
customers: DS.hasMany('module'),
});
// models/customer.js
import DS from 'ember-data';
export default DS.Model.extend({
producers: DS.hasMany('module'),
});
Right now I am working with fixtures, and what I do is just define the customers
and producers
arrays with the ids of the "targets". The question is, that sooner or later I will switch to a real REST backend, and that means I have to deal with the model mismatch. I think I have to options:
Define a Ember model
customer2producer
. This will probably work, but I guess I will have to write a lot of boilerplate, and most probably the relationships will not be accessible from the model (customer.get('producers')
won't work directly, I would probably have to play with and do something like in this question:Hook into the serializer/deserializer system and translate the backend
customer2producer
objects into Ember model references. I am not completely sure this is possible, but I would say it is from reading quickly these SO questions:How to save models with many-to-many relationship in Ember.js?
I guess this is a very common scenario, and I would like to know what is the cleanest way to approach the problem. Thanks in advance.