简体   繁体   English

ember.js与每个助手一起使用itemController不能按预期工作

[英]ember.js using itemController with each helper doesnt work as expected

How should i change the following code to work as expected, so doesnt change the completedness of other todos if i completed one? 我应该如何更改以下代码以使其按预期工作,所以如果我完成了一个待办事项,是否也不会更改其他待办事项的完整性?

itemController="todo" claimed to wrap each item in an own controllers but fails to do so. itemController =“ todo”声称将每个项目包装在自己的控制器中,但没有这样做。

index.html index.html

    <script type="text/x-handlebars" id="todos">
    <ul>
        {{#each controller itemController="todo"}}
        <li>{{#link-to 'todo' this}}{{job}} -- {{#if isCompleted}}Completed{{else}}Incomplete{{/if}}{{/link-to}}</li>
        {{/each}}
    </ul>
    {{outlet}}
</script>
<script type="text/x-handlebars" id="todo">
    <p>Job: {{job}} -- {{#if isCompleted}}Completed{{else}}Incomplete{{/if}}</p>
    <button {{action 'complete' controller}}>Complete</button>
</script>

app.js app.js

App = Ember.Application.create();

App.Router.map(function() {
  this.resource('todos', function() {
    this.resource('todo', { path: ':todo_id' })
  });
});

App.IndexRoute = Ember.Route.extend({
    redirect: function() { this.transitionTo('todos'); }
});

App.TodosRoute = Ember.Route.extend({
    model: function() {
        return todos;
    }
});

App.TodoRoute = Ember.Route.extend({
    model: function(params) {
        return todos.findBy('id', params.todos_id);
    }
});

App.TodosController = Ember.ArrayController.extend({
});

App.TodoController = Ember.ObjectController.extend({
    isCompleted: false,
    actions: {
        complete: function() {
            this.set('isCompleted',true);
        }
    }
});

var todos = [{id: '1', job: 'running'}, {id: '2', job: 'swimming'}, {id: '3', job: 'study'}];

I believe that you are mixing things 我相信你在混合事物

1 you have a list of todos each backed by ist own controller in the App.TodosRoute but in the App.TodoRoute you have another instance of the todoController, since the property is at the controller level, you are viewving the property setted for the 4th instance of the controller, the one reponsible for the todoRoute that is singleton. 1您在App.TodosRoute中有一个待办事项列表,每个待办事项都由自己的控制器支持,但是在App.TodoRoute中,您有另一个todoController实例,因为该属性位于控制器级别,因此您正在查看为第4个设置的属性控制器的实例,负责todoRoute的实例是单例。

You can move the property to the model and everything will goes well. 您可以将属性移至模型,一切都会顺利进行。

App.TodoController = Ember.ObjectController.extend({

    actions: {
        complete: function() {
            this.set('isCompleted',true);
        }
    }
});

var todos = [{id: '1', job: 'running',isCompleted: false}, {id: '2', job: 'swimming',isCompleted: false}, {id: '3', job: 'study',isCompleted: false}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM