lundi 10 août 2015

How to deal with many2many objects in EmberJS client?

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:

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.

Aucun commentaire:

Enregistrer un commentaire