I have a situation where the same model Event
should be sent by the backend server:
- list of events by country (CountryEventsController hit:
countries/:country_id/events
) - list of events by shop (ShopEventsController hit:
shops/:shop_id/events
)
Finally, the two controllers send an array of the same model Event
.
On Ember side I declared 2 models:
- Shop
- Event
and connected them with hasMany
relations on both sides.
To fetch country events, I use Event
model and to fetch Shop events, I had to create a separate model shop-event
:
model() {
return RSVP.hash({
events: this.store.query('event', { country_id: this.get('currentShop.shop.country.id')}),
shopEvents: this.store.query('shop-event', { shop_id: this.get('currentShop.shop.id') })
});
},
The questions I have:
- Isn’t it a duplication to declare the same model?
- What attributes should I put in the second model ?
- Finally when getting the same model type, I have to delete an Event from Shop association. How to do that to be able to call
ShopEventsController
? If I callshopEvent.destroyRecord();
it will callEventsController#destroy
action and delete completely the Event from the database.
I defined adapters for shop-event
and event
, - this way I can create/edit/delete Country events and create/delete only ShopEvents by keeping Country events untouched.
The creation works:
let shopEvent = this.store.createRecord('shopEvent', {
shop: this.currentShop.get('shop'),
event: this.store.peekRecord('event', event.get('id')),
});
shopEvent.save();
But how to delete ?
Actually, I declared just 2 relations belongsTo
in shop-event
model:
#models/shop-event.js
export default DS.Model.extend({
shop: DS.belongsTo(),
event: DS.belongsTo()
});
but don’t think it to be correct, because it is not a join-table ShopsEvents. How to do in such a situation ? Thank you