2013-10-28 38 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。