F
F
fsamorodov2015-11-02 20:15:13
Ember.js
fsamorodov, 2015-11-02 20:15:13

How to implement createRecord in emberJS for models that have hasMany and belongsTo?

As a backend I use emberFire at the time of development (ember v2.1).
There are two models:
Categories:
2Nya8Lx2a_w.jpg
Services: The
0lD2CTnC2yg.jpg
page route loads one of the selected categories into the model:
model: function (params) {
return this.store.find('services-cat', params.category_id)
}
Action to create a new service for the selected category is:
BD3l0SrLzek.jpg
How to remake the action so that the service is saved and the category is updated according to the hasMany and belongsTo rules?
I took the serializers from the emberFire docks.
export default FirebaseSerializer.extend({
attrs: {
category: { embedded: 'always' }
}
});
and
export default FirebaseSerializer.extend({
attrs: {
services: { embedded: 'always' }
}
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Bondarenko, 2015-12-09
@BondbonD

You need to save both services-cat and category.
Something like this:
action:
__createServece: function(model) {
____var servicesCat = this.store.createRecord('services-cat', {
______servName: model.servName,
______time: ...
______...
____});
____var service = this.store.createRecord('service', {
______...
______category: servicesCat
____});
// in a simple case, save services-cat and category
__service.save();
__servicesCat.save();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question