我正在关注 Ember 的 TodoMVC 教程,但我被卡住了。基本上,我定义了 2 个控制器。这是 todos.js
import Ember from "ember";
export default Ember.ArrayController.extend({
actions:{
createTodo: function(){
var title = this.get("newTitle");
if(!title){
return false;
}
if(!title.trim()){
return;
}
var todo = this.store.createRecord("todo", {
title: title,
isCompleted: false
});
// Clear text field
this.set('newTitle', '');
todo.save();
}
}
})
这是 todo.js
import Ember from "ember"
export default Ember.ObjectController.extend({
isCompleted: function(key, value){
var model = this.get("model");
if(value === undefined){
return model.get("isCompleted");
} else {
model.set('isCompleted', value);
model.save();
return value;
}
}.property('model','model.isCompleted')
});
这是路线/todos.js
import Ember from "ember";
export default Ember.Route.extend({
model: function() {
return this.store.find("todo");
}
});
最后,还定义了 todos.hbs
<ul id="todo-list">
{{#each todo in model itemController="todo"}}
<li {{bind-attr class="todo.isCompleted:completed"}}>
{{input
type="checkbox"
class="toggle"
checked=todo.isCompleted
}}
<label>{{todo.title}}</label><button class="destroy"></button>
</li>
{{/each}}
</ul>
一切看起来都不错,但我在控制台中收到以下错误:
未捕获的错误:断言失败:#each 循环的值必须是一个数组。你通过了 todomvc-embercli@controller:array:,但它应该是一个 ArrayController
我在这里做错了什么?