Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing the model from the template

Tags:

ember.js

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 Member object, corresponding MemberRoute, MemberView classes and a template with the name member.
  • The Member object had some attributes such as id, nickname, etc.
  • NOTE: no controller of the form MemberController was 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 MemberController defined 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

like image 851
Patrick Hammer Avatar asked Jan 23 '13 23:01

Patrick Hammer


1 Answers

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.

like image 154
Mike Grassotti Avatar answered Sep 28 '22 15:09

Mike Grassotti