john.stein
john.stein

Reputation: 535

Why do I have to specify model.name instead of just name?

Why do I have to specify model.name in the template below? I was under the impression that a controller decorates its model. I thought I should be able to do just {{ name }} in the template below. But it only works if I do {{ model.name }}.

Any help greatly appreciated. Thanks!

app.js

App = Ember.Application.create({});

App.Router.map(function () {
  this.resource("posts", { path: "/" }, function () {
    this.route("new", { path: "/new" });
    this.route("post", { path: ":post_id" });
  });
  this.route("another", { path: "/another" });
});

App.PostsRoute = Ember.Route.extend({
  model: function(params) {
    return [{ name: "foo" },{ name: "bar" },{ name: "zoo" }]
  }
});

App.PostsPostRoute = Ember.Route.extend({
  model: function(params) {
    console.log('model');
    return Ember.Object.create({ name: "MODEL name" })
  }
});

App.PostsPostController = Ember.Controller.extend({
  selected: false
});

index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ember Test</title>

</head>
<body>

<script type="text/x-handlebars" id="posts">
  posts here
  {{#each controller}}
  {{name}}
  {{/each}}
  {{ outlet }}
</script>

<script type="text/x-handlebars" id="posts/index">
  posts index here
</script>

<script type="text/x-handlebars" id="posts/new">
  posts new here
</script>

<script type="text/x-handlebars" id="posts/post">
  here is a post???
  {{ model.name }}
  {{ selected }}
</script>

<script src="jquery-1.10.2.js"></script>
<script src="handlebars.js"></script>
<script src="ember.js"></script>
<script src="app.js"></script>
</body>
</html>

Upvotes: 0

Views: 58

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

This happen because your PostsPostController is extending Ember.Controller, to get this working you need to extend Ember.ObjectController.

App.PostsPostController = Ember.ObjectController.extend({
  selected: false
});

In the docs this is the definition of Ember.ObjectController:

Ember.ObjectController is part of Ember's Controller layer. It is intended to wrap a single object, proxying unhandled attempts to get and set to the underlying content object, and to forward unhandled action attempts to its target.

I put your sample in this jsfiddle http://jsfiddle.net/marciojunior/nWTqN/, please give a look.

Upvotes: 3

Related Questions