Should a CreateShoppingListItem component ever need to know about the Ember Data Store?


#1

I’d like to discuss the ideal design pattern/delegation of responsibilities when it comes to creating and persisting a new Ember Data model.

Specifically, what are the duties of the Route vs the duties of the Component when it comes to creating and persisting a new model.

Scenario: Im building a Shopping List app which allows you to view your shopping list and append items to the list.

I have a single Model called a

ShoppingListItem
   - name     # string
   - quantity # int

and the following parts

ShoppingListRoute
ShoppingListTemplate
    ShoppingListItemComponent
    CreateShoppingListItemComponent

What I am not sure about is

  • Should the CreateShoppingListItemComponent ever need to know about the Ember Data Store
  • Should Ember Data Models only be created by Routes?
  • Should Ember Data Models only be persisted/saved by Routes?
  • Should the CreateShoppingListItemComponent bubble up a new ShoppingListItem to the route action where it is saved?
  • Or should the CreateShoppingListItemComponent bubble up a plain old javascript dictionary of values up to a route action where it is converted into a new ShoppingListItem to be saved?

Is there an official stance here? I feel like I could achieve this in several ways but am sure there is a best practice.

Many thanks! Oli


#2

I think you could delegate responsibility for creating records to the route; I also don’t think it’s a sin if you didn’t in the component either.

But, I think this is probably a great place to consider using a service. That way, the cart is accessible to anything that wants to use it — and, you can isolate all cart-related logic to that place (including creating the Ember data models).

Also, it’s probably worth checking out what DockYard did here: https://github.com/DockYard/ember-cart


#3

Very good point. I’m lerning react reacently and the component don’t know how to save. It’ll receive the creactAction ( or you could booble it in ember ). So the Responsability to create is from route, no doubt about that. The CreateComponent never know about Ember Data. You could at some moment stop to use Ember Data Store, so your component will be unless to you and will need to be refactored.

Yeph you could achieve this from several ways.