我实际上关注了exact procedure to produce ToDOMVC制作这个,但我无法弄清楚为什么我会收到以下错误:
Assertion failed: The value that #each loops over must be an Array.
You passed (generated questions controller) ember.js:394 Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'
下面是代码
index.html
<script type="text/x-handlebars" data-template-name="questions"><!--ACW-not sure should be question or equizz-->
<ul id="question-list" >
{{#each}}
<li>
<h3>{{title}}</h3>
</li>
<li>
<p>{{desc}}</p>
</li>
{{/each}}
</ul>
</script><!--template END-->
application.js
window.Equizz = Ember.Application.create();
Equizz.ApplicationAdapter = DS.FixtureAdapter.extend();
路由器.js
Equizz.Router.map(function () {
this.resource('questions', { path: '/' });
});
Equizz.EquizzRoute = Ember.Route.extend({
model: function () {
return this.store.find('question');
}
});
问题.js
Equizz.Question = DS.Model.extend({
qid: DS.attr('string'),
category: DS.attr('string'),
type:DS.attr('string'),
title: DS.attr('string'),
desc: DS.attr('string'),
diff_level: DS.attr('string'),
answer: DS.attr('boolean')
});
Equizz.Question.FIXTURES = [
{
qid: '1',
category: 'Track',
type:'True & False',
title: 'Get 100 in the quizz is the most disgraced act in simulator lab.',
desc: 'think clearly, you should know the answer without use your brain...',
diff_level: 'Hard',
answer: false
},
{
qid: '2',
category: 'Common',
type:'True & False',
title: 'You are allowed to eat in simulator lab.',
desc: 'Like what? Halal?',
diff_level: 'Medium',
answer: false
},
{
qid: '3',
category: 'BS',
type:'True & False',
title: 'fsafasf asf asjfkl; as fkasl; faf a;sf sf asfl; sjlfjs a; fsl fas;f dsaf aslfj asl;fj a;fj alfj slafj a?',
desc: 'Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?Like what? Halal?',
diff_level: 'Easy',
answer: true
}
];
最佳答案
您没有正确声明您的路线。对于问题路由,您需要创建 Equizz.QuestionsRoute,而不是 Equizz.EquizzRoute。
为了能够在模板中使用 {{#each}}, Controller 必须是 ArrayController。如果相应的路由从模型 Hook 返回一个数组,则自动生成的 Controller 应该就是这样。由于您没有 QuestionsRoute 返回数组模型,因此 Ember 正在生成默认的非数组 Controller 。
您应该阅读docs regarding routes ,解释了整个路由/ Controller / View 命名方案。
关于jquery - 在 emberjs 中加载带有夹具数据的路线时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635961/