2013-10-28 39 views
3

我居然也跟着exact procedure to produce ToDOMVC,使这一个,但我想不出世界上,为什么我收到以下错误夹具数据加载路线:错误而在emberjs

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(); 

router.js

Equizz.Router.map(function() { 
    this.resource('questions', { path: '/' }); 
}); 

Equizz.EquizzRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find('question'); 
    } 
}); 

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 
} 
]; 

回答

0

您没有正确申报您的路线。对于问题路线,您需要创建Equizz.QuestionsRoute,而不是Equizz.EquizzRoute。

为了能够在模板中使用{{#each}},控制器必须是一个ArrayController。如果相应的路由从模型钩子返回一个数组,自动生成的控制器应该是这样的。由于您没有返回数组模型的QuestionsRoute,因此Ember正在生成默认的非数组ish控制器。

您应该阅读docs regarding routes,解释整个路由/控制器/视图命名方案。

+0

但是TodoMVC如何能够在没有控制器的情况下做到这一点? – Ezeewei

+0

btw,现在我在加载路由时遇到错误: 错误 消息:“id属性必须定义为夹具的数字或字符串{qid:1,category:Track,类型:True&False,title:在模拟器实验室中获得100分,是模拟器实验室中最糟糕的行为。,desc:清楚地思考,你应该知道答案,而不用你的大脑......,diff_level:Hard,回答:false}“ stack:(...) – Ezeewei

+1

如果没有提供路由器,Ember会为路由生成控制器。如果路由的模型是一个数组,则路由必须从模型钩子提供一个数组。 在Todo示例中,路由提供了一个数组模型,所以Ember知道为该路由生成一个ArrayController。 另外,灯具需要一个'id'属性,你的被定义为'qid'。 – rallrall

0

Ember.js依赖于它自己的一组naming conventions。路线'问题'有一个名为QuestionRoute的Ember.Route。因此,

Equizz.EquizzRoute => Equizz.QuestionRoute。

Ember灯具也有自己的"naming" conventions,并且需要有唯一的ID。因此

qid => id 

这也意味着烬有它的关联模型IDS的概念本身,因而不需要模型的自定义ID。

0

我和其他教程一起遇到了同样的问题。

就像@Dawson提到的,夹具适配器遵循与REST适配器不同的规则。下面是从link @Dawson相关文字提供:

NAMING CONVENTIONS

Unlike the REST Adapter, the Fixture Adapter does not make any assumptions about the naming conventions of your model. As you saw in the example above, if you declare the attribute as firstName during DS.Model.extend, you use firstName to represent the same field in your fixture data.

Importantly, you should make sure that each record in your fixture data has a uniquely identifiable field. By default, Ember Data assumes this key is called id. Should you not provide an id field in your fixtures, or not override the primary key, the Fixture Adapter will throw an error.

要解决此问题:

假设我的模式被称为Itemitem.js

import DS from 'ember-data'; 

var Item = DS.Model.extend({ 
    title: DS.attr('string'), 
    description: DS.attr('string'), 
    price: DS.attr('number'), 
    qty: DS.attr('number'), 
    images: DS.attr('string'), 
    sold: DS.attr('boolean'), 
}); 

我建我的灯具数据另一个对象:

//Fixtures require an ID field not supplied by the DS.model 
//write the fixtures to an object then append IDs in a loop later 
var f = [ 
     { 
      title: "Nesting Tables", 
      description: "Set of 3 matching nesting tables in a cherry finish", 
      price: "25", 
      qty: 1, 
      images: '"DSC_0552.JPG"', 
      sold: false 
     }, 
     { 
      title: "Brown Leather Storage Ottomans", 
      description: "Nicely finished with nail-head rivets.", 
      price: "45", 
      qty: 2, 
      images: '"DSC_0554.JPG","DSC_0556.JPG"', 
      sold: false 
     }, 
     { 
      title: "Black 3 shelf bookcase", 
      description: "Handsome black wood bookcase with 3 shelves", 
      price: "40", 
      qty: 1, 
      images: '"DSC_0559.JPG","DSC_0557.JPG"', 
      sold: false 
     } 
]; 

然后使用一个循环ID添加到每个元素:

// here is our loop to add IDs to the fixtures 
var c = f.length; 
for (var i=0; i<c;i++) 
{ 
    f[i].id = i; 
} 

然后分配完成的f对象的FIXTURES对象模型:

Item.reopenClass({ 
    FIXTURES: f 
}); 

这样,灯具看起来像我的最后REST数据,但我有必要的部分来使用FixtureAdapter。