ed94133
ed94133

Reputation: 1575

In Ember, why does my template want {{model.key}}, not just {{key}}?

I am trying out Ember, and finding a discrepancy with the docs. I used the Ember CLI to ember generate template index and ember generate route index. Then I set up a trivial model in index.js:

model: function () {
    return {name: "Joe"};
}

From my reading of the docs and examples, I expected to be able to access this value simply with {{name}} in my index.hbs template, but instead I only get the value with {{model.name}}. Why?

Upvotes: 0

Views: 32

Answers (2)

artych
artych

Reputation: 3669

Before Ember 1.11 you could use ObjectController, that works like a proxy to corresponding route model, and you could write {{name}} for model.name.

ObjectController was deprecated in Ember 1.11, details here: http://emberjs.com/deprecations/v1.x/#toc_objectcontroller. So in last Ember versions you should use Controller class instead ObjectController, that doesn't work as proxy of model. You could think of it as of Ember Object with model property from corresponding route. So {{name}} means property of Controller, {{model.name}} - property of model.

For example:

//route
model: function () {
  return {name: "Joe"};
}

 //controller
 import Ember from 'ember';
 export default Ember.Controller.extend({
   name: 'Marry'
 });

//template
{{name}} //=> Marry
{{model.name}} //=> Joe

Upvotes: 2

user156888
user156888

Reputation:

I think this might be a thing about explicitness but I'm not 100% sure - you can also have data sent to the template on a property other than model so it might be about allowing that to be more easily understood - model is a poor property name IMO anyway

You could use the with helper if the syntax is too verbose for you:

{{#with story}}
    <div class="intro">{{{intro}}}</div>
    <div class="body">{{{body}}}</div>
{{/with}}

Upvotes: 0

Related Questions