我有一个简单的 ember.js hello world,只有 1 条路线
(function() {
window.PersonApp = Ember.Application.create({
ApplicationController: Ember.ObjectController.extend({}),
ApplicationView: Ember.View.extend({
templateName: 'application'
}),
Person: Ember.Object.extend({
username: null,
isActive: false
}),
PersonController: Ember.ArrayController.extend({
content: [],
addPerson: function(username) {
var person = PersonApp.Person.create({ username: username });
this.pushObject(person);
},
removePerson: function(person) {
this.removeObject(person);
}
}),
AddPersonTextField: Ember.TextField.extend({
insertNewline: function() {
var value = this.get('value');
if (value) {
PersonApp.PersonController.addPerson(value);
this.set('value', '');
}
}
}),
RemovePersonCheckbox: Ember.Checkbox.extend({
content: null,
change: function(event) {
PersonApp.PersonController.removePerson(this.content);
},
}),
HomeController: Ember.Controller.extend(),
HomeView: Ember.View.extend({
templateName: 'home'
}),
Router: Ember.Router.create({
root : Ember.Route.extend({
index: Em.Route.extend({
route: '/',
connectOutlets: function(router){
router.get('applicationController').connectOutlet('home');
}
})
})
})
});
$(function() {
PersonApp.initialize(PersonApp.Router);
});
})();
我的 html 模板非常基本,只定义了一个出口和 1 个“主页”视图以按用户名显示用户
<!DOCTYPE html>
<html>
<title>Users</title>
<link href="{{ STATIC_URL }}css/default.css" rel="stylesheet" type="text/css">
<script src="{{ STATIC_URL }}script/vendor/jquery-1.7.2.js" type="text/javascript"></script>
<script src="{{ STATIC_URL }}script/vendor/handlebars-1.0.0.beta.6.js" type="text/javascript"></script>
<body>
{% load templatetag_handlebars %}
<script type="text/x-handlebars" data-template-name="application">
<div class="container">
{% verbatim %}
{{outlet}}
{% endverbatim %}
</div>
</script>
<script type="text/x-handlebars" data-template-name="home">
{% verbatim %}
{{view PersonApp.AddPersonTextField placeholder="username"}}
<ul>
{{#each PersonApp.PersonController}}
<li>
<label>
{{view PersonApp.RemovePersonCheckbox contentBinding="this" checkedBinding="isActive"}}
{{username}}
</label>
</li>
{{/each}}
</ul>
{% endverbatim %}
</script>
<script type="text/javascript" >
ENV = {
CP_DEFAULT_CACHEABLE: true,
VIEW_PRESERVES_CONTEXT: true
};
</script>
<script src="{{ STATIC_URL }}script/vendor/ember-1.0.pre.js" type="text/javascript"></script>
<script src="{{ STATIC_URL }}script/app/person.js" type="text/javascript"></script>
</body>
</html>
当它运行时,我收到错误
未捕获的错误:断言失败:Ember.CollectionView 的内容必须实现 Ember.Array。你通过了 PersonApp.PersonController
如果我尝试将 Ember.ArrayController.extend 切换到 Ember.ArrayController.create 我收到错误
未捕获的类型错误:对象没有“创建”方法
环顾四周后,似乎最新的 ember 可能希望我首先在 ArrayController 上调用 .create({}),然后是 .extend({}),但我找不到任何使用 1.0 前版本的好例子。有谁知道我在上面可能做错了什么?