Patrick Hammer
Patrick Hammer

Reputation: 1172

accessing the model from the template

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

Setup B - The customization

The strange behaviour (resp. what I do not completely understand)

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

or

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

Upvotes: 5

Views: 2955

Answers (2)

ken
ken

Reputation: 3745

an ObjectController acts as proxy to the object set to the controller's content. When no controller is defined, Ember will create a controller for you and set its content by default to whatever object is returned by the model() function, if defined, in the route. The behaviour should be the same whether you define your own controller or let Ember define one for you

The default context in the template is the controller itself i.e. this = an instance of your controller or the generated one. When you try to access nickname in that context, Ember will first try to resolve it against the controller itself and if nothing is found, it resolves it against its content, i.e the object if you already manually set it to the controller's content.

Finally, there is no default implementation of the model() function in the Route except when you're using dynamic urls, say /foo/id that resolves against /foo/:id, Ember uses the id provided to load a Foo object with the id provided, thus providing a default implementation to the model() function. At the end it boils down to the same mechanism, only automated for your convenience.

I suggest you listen to this for more insights on how things are automated for you by Ember. But when it comes to the content being displayed, there is no magic you have to manually wire the content of the controller.

Upvotes: 0

Mike Grassotti
Mike Grassotti

Reputation: 19050

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.

It's hard to say for sure without seeing your code, but my best guess is that your MemberController extends Ember.Controller. The default provided by ember (in this scenario) would have been an Ember.ObjectController. If that's what you want, change your MemberController definition to:

App.MemberController = Ember.ObjectController.extend({
  myProperty: 'value'
});

An objectController acts as a proxy to it's content property, 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:

{{content.id}} or {{content.nickname}}

it's a sign that you should change to an ObjectController. See EMBER GUIDES: REPRESENTING A SINGLE MODEL! for a more detailed explanation.

Upvotes: 8

Related Questions