2

我应该如何更改以下代码以按预期工作,所以如果我完成了一个待办事项,则不会改变其他待办事项的完整性?

itemController="todo" 声称将每个项目包装在自己的控制器中,但没有这样做。

索引.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>

应用程序.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'}];
4

1 回答 1

0

我相信你在混合东西

1 您在 App.TodosRoute 中有一个由自己的控制器支持的待办事项列表,但在 App.TodoRoute 中您有另一个 todoController 实例,因为该属性处于控制器级别,您正在查看为第 4 个设置的属性控制器的实例,负责单例的 todoRoute。

您可以将属性移动到模型中,一切都会顺利进行。

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}];
于 2013-10-14T15:38:26.597 回答