Playing around with ember, I found that sometimes the model is stored on the controller’s content property, sometimes the model is directly available on the controller as well. I do not understand however, when this is the case.
Let me explain it by an example which I found when assembling my ember MVC.
Setup A – The start
- I defined a custom
Memberobject, correspondingMemberRoute,MemberViewclasses and a template with the namemember. - The
Memberobject had some attributes such asid,nickname, etc. - NOTE: no controller of the form
MemberControllerwas defined, thus by ember’s convention, it provides the controller on its own.
Setup B – The customization
- Same as setup A, but now there is a
MemberControllerdefined that contains some action methods that are triggered from within the template.
The strange behaviour (resp. what I do not completely understand)
- in setup A, I can refer to the
Member‘s attributes directly with{{id}}or{{nickname}}. - in setup B, I have to use
{{content.id}}or{{content.nickname}}
As documented in ember’s documentation, MemberView does
setupController : function(controller, member) {
controller.set('content', member);
},
So, could somebody help me to understand why the difference and where the difference is? Currently, my guess would be either
- that the context of the template is different (possibly there is a code piece missing in the setup of the controller?)
or
- the default controller that is provided by ember automatically, has some additional magic that is not directly avaiable for customized controllers.
Any help to understand this is highly appreciated. It already took my quite a while to come as far as this. I first thought it could be the modularization introduced by the project setup with requireJS (well, I still think that could have a influence). Ember is v1.0pre4.
Thanks in advance!
Patrick
It’s hard to say for sure without seeing your code, but my best guess is that your
MemberControllerextendsEmber.Controller. The default provided by ember (in this scenario) would have been anEmber.ObjectController. If that’s what you want, change your MemberController definition to:An objectController acts as a proxy to it’s
contentproperty, typically that is an ember model. So if things are wired up correctly you should never need to access a model via the ‘content` property. If you ever see something like:it’s a sign that you should change to an ObjectController. See EMBER GUIDES: REPRESENTING A SINGLE MODEL! for a more detailed explanation.