Title: Set controller for template rendering
Tags: ember.js
Question: I recently decided to look into Ember.js after having spent the last two years with KO. The first thing to notice is that the complexity seems a TAD steeper but I shall prevail :)
Now, I seem to need to hardcode the controller for a certain template which seems weird:
App.IndexRoute = Ember.Route.extend({
renderTemplate: function() {
this.render('todosList', { into: 'application' });
App.todosController = Ember.ArrayController.create({
content: [App.Todo.create(), App.Todo.create()]
<script type="text/x-handlebars" data-template-name="application"&gt;
{{outlet}}
</script&gt;
<script type="text/x-handlebars" data-template-name="todosList"&gt;
<ul id="todo-list"&gt;
{{#each todo in App.todosController.content}}
<li&gt;
<label {{bindAttr class="todo.isCompleted"}}&gt;{{view Ember.Checkbox checkedBinding="todo.isCompleted"}} {{todo.title}}</label&gt;
<button {{action 'removeTodo' todo target="App.todosController"}}&gt;Ta bort</button&gt;
</li&gt;
{{/each}}
</ul&gt;
{{view Ember.TextField valueBinding="App.todosController.newTodoText"}}
<button {{action 'newTodo' App.todosController.newTodoText target="App.todosController"}}&gt;New todo</button&gt;
</script&gt;
I tried setting controller: 'App.todosController' in the render() call but nothing. The #each in the view accepts nothing else than App.todosController.content which doesn't seem right. Why do I even need to explicitly state that it's the content it should read, isn't that set automatically?
Thankful for any help, Ember seems to have its niceties but in the beginning much is confusing.
Comment: you should add route for your controller

Comment: I don't cared to elaborate :)

Here is the accepted answer: Short answer first:
working jsbin: http://jsbin.com/usaluc/8/edit
Longer answer:
You had some misconceptions in your code that I've changed to be more ember-like, this results in this very simple example.
todosList template
<script type="text/x-handlebars" data-template-name="todosList"&gt;
<ul id="todo-list"&gt;
{{#each todo in controller}}
<li&gt;
<label {{bindAttr class="todo.isCompleted"}}&gt;
{{view Ember.Checkbox checkedBinding="todo.isCompleted"}} {{todo.title}}
</label&gt;
<button {{action 'removeTodo' todo target="controller"}}&gt;Remove toto</button&gt;
</li&gt;
{{/each}}
</ul&gt;
{{view Ember.TextField valueBinding="newTodoText"}}
<button {{action 'newTodo' newTodoText target="controller"}}&gt;New todo</button&gt;
</script&gt;
IndexRoute
When using renderTemplate to make sure the correct controller is used you should define it in the hash you are passing into the render function:
App.IndexRoute = Ember.Route.extend({
renderTemplate: function() {
this.render('todosList', {
into: 'application',
controller: 'todosList'
});
Route map
Since you dind't post your route map, and furthermore because your are using the IndexRoute renderTemplate hook I assumed your todosList is rendered directly when visiting '/', so just to be concise here a simple router map the renders the todosList template when visiting '/'
App.Router.map(function() {
this.resource('todosList', {path: '/'});
TodosListRoute
Now that you have a TodosListRoute in where you want to set to correct controller content, you should hook into the setupController function and do just that:
App.TodosListRoute = Ember.Route.extend({
setupController: function(controller, model) {
var myTodos = [
App.Todo.create({title: 'Drink water', text:'foo'}),
App.Todo.create({title: 'Bring out the trash', text:'bar'})
];
controller.set('content', myTodos);
TodosListController
The TodosListController looks fairly simple so far only including the two functions newTodo and removeTodo using the title value passed from the action helper in your template:
App.TodosListController = Ember.ArrayController.extend({
newTodo: function(title) {
var todo = App.Todo.create({title: title, text:'foo'});
this.get('content').pushObject(todo);
removeTodo: function(todo) {
this.get('content').removeObject(todo);
Hope it helps.
Comment for this answer: It sure helped, thanks! I'm gonna spend some time going over your code and see where I went wrong.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/29/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ember.js>', 'Title': 'Set controller for template rendering', 'ViewCount': 4787, 'id': 18507336, 'user_AboutMe': '', 'user_AccountId': 1470301.0, 'user_DisplayName': 'grilledtomatos', 'user_Reputation': 39.0, 'user_Views': 13.0, 'user_id': 1382035.0}}