A
A
Andy Developer2016-09-24 15:41:25
Ember.js
Andy Developer, 2016-09-24 15:41:25

How to make different serializers for the same Ember.js model?

Ember.js 2.7 pods
have the following
/pods
/pods/order/model.js
/pods/order/serializer.js
/pods/order/index/route.js
/pods/order/index/template.hbs
/pods/order/ edit/route.js
/pods/order/edit/template.hbs
/pods/order/new/route.js
/pods/order/new/template.hbs
router.js

this.route('order', { path: '/dashboard/order/' }, function(){
    this.route('new');
    this.route('edit', { path: '/:order_id/edit' });
  });

model.js
import DS from 'ember-data';

export default DS.Model.extend({
    customer: DS.belongsTo('customer',  {async: true}),
});

serializer.js
import RESTSerializer from 'ember-data/serializers/rest';
import DS from 'ember-data';

export default RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    customer: {embedded: 'always'},
  }
});

How to make sure that there is no customer in the index route, and for example, there is already a customer in the edit route, api returns accordingly, for api / orders without customer, and for api / orders / 2 already with customer
Tried to put serialize.js in each folder(index, edit, new), but that doesn't work.
I hope I explained clearly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Romanov, 2016-09-24
@Kaer_Morchen

I tried to put serialize.js in each folder (index, edit, new), but it doesn't work like that.

I didn't work much with RESTSerializer, but it definitely won't work that way.
Maybe it's easier not to insert the customer into the output for order, but to load it as needed (only in edit) with a separate request?
Why is this behavior even necessary?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question