doub1ejack
doub1ejack

Reputation: 11171

Ember.js: Where/when/why declare properties in model or controller?

It seems like I can declare computed properties in the model and in the controller. I'm getting to the point where I'm not sure which one to look in for a given property.

What dictates whether a property should be placed in the controller vs the model?

Upvotes: 0

Views: 107

Answers (1)

Peter Wong
Peter Wong

Reputation: 491

Typically, place in the controller if the property is presentational in nature (e.g. display, labels, formatting), and place in the model if the property is inherent to the record itself (e.g. calculations, associations)

In practical terms though:

Model if the property

  • needs to be accessed by other models, since models don't have access to their controllers
  • needs to be accessed in routes before controllers are setup
  • needs to persist across controllers (e.g. order.subtotal is used in OrderNewController and OrderController)

Controller if the property

  • only needs to be accessed by the view or template
  • only needs to be accessed by other controllers

You can probably go with putting most properties in the controller, until you run into situations where you need to access the property from other models, or if you find yourself writing {{controllers.modelName.property}} too many times.

Upvotes: 2

Related Questions