简体   繁体   English

如何在Ember.js模板中显示hasMany记录?

[英]How to display hasMany records in Ember.js template?

I have model defined like this: 我有这样定义的模型:

App.Question = DS.Model.extend({   
    title: DS.attr( 'string' ),  
    answers: DS.hasMany('App.Answer') 
});

App.Answer = DS.Model.extend({   
    title: DS.attr( 'string' ),  
    wynikid: DS.attr( 'number' ) 
});

And get data from RESTAdapter that returns data like this: 并从RESTAdapter获取返回如下数据的数据:

{
    "questions": [
        {
            "id": 77,
            "title": "O której wstajesz?",
            "answers": [
                {
                    "id": 159,
                    "title": "O godzinie 6",
                    "wynikid": 57
                },
                {
                    "id": 160,
                    "title": "O godzinie 7",
                    "wynikid": 56
                },
                {
                    "id": 161,
                    "title": "O godzinie 12",
                    "wynikid": 55
                }
            ]
        },
        {
            "id": 76,
            "title": "Kot czy kominiarz?",
            "answers": [
                {
                    "id": 156,
                    "title": "Kocur",
                    "wynikid": 57
                },
                {
                    "id": 157,
                    "title": "Kominiarz",
                    "wynikid": 56
                },
                {
                    "id": 158,
                    "title": "Ani to ani to",
                    "wynikid": 55
                }
            ]
        },
        {
            "id": 75,
            "title": "Wybierz ulubione imię",
            "answers": [
                {
                    "id": 153,
                    "title": "Bożydar",
                    "wynikid": 57
                },
                {
                    "id": 154,
                    "title": "Aleksander",
                    "wynikid": 56
                },
                {
                    "id": 155,
                    "title": "Andrzej",
                    "wynikid": 55
                }
            ]
        },
        {
            "id": 74,
            "title": "Wybierz liczbę",
            "answers": [
                {
                    "id": 152,
                    "title": "Liczba 13",
                    "wynikid": 56
                },
                {
                    "id": 151,
                    "title": "Liczba 7",
                    "wynikid": 55
                },
                {
                    "id": 150,
                    "title": "Liczba 1",
                    "wynikid": 57
                }
            ]
        },
        {
            "id": 78,
            "title": "Ulubiona pora roku",
            "answers": [
                {
                    "id": 162,
                    "title": "To lato",
                    "wynikid": 57
                },
                {
                    "id": 163,
                    "title": "Jesień / Wiosna",
                    "wynikid": 56
                },
                {
                    "id": 164,
                    "title": "To zima",
                    "wynikid": 55
                }
            ]
        }
    ]
}

Now what i would like to acomplish is to show a list like this: 现在我想完成的是显示一个这样的列表:

O której wstajesz?
 O godzinie 6
 O godzinie 7
 ...
Kot czy kominiarz?
 ...

I've tried to use this in Ember.js template: 我试图在Ember.js模板中使用它:

 {{#each controller}}

  {{title}}<br>

    {{#each answer in answers}}

    \t{{title}}<br>

    {{/each}}

  {{/each}}

And questions are displayed but answers arent. 并显示问题,但没有回答。 I don't have any error in console and JSONLint also does not see any problems with JSON. 我在控制台中没有任何错误,并且JSONLint也看不到JSON的任何问题。

How to make it work? 如何使其运作?

I use: 我用:

handlebars1.0.0.0.js ember-1.0.0.0.min.js ember data: v0.13-238-gbf84978 handlebars1.0.0.0.js ember-1.0.0.0.min.js余烬数据:v0.13-238-gbf84978

The newest version of ember/ember-data does not support embedded records. 最新版本的ember / ember-data不支持嵌入式记录。 This is documented here , but I found the workaround to be very clumsy and couldn't get it to work myself. 这是在此处记录的 ,但是我发现解决方法非常笨拙,无法自己使用。

Maybe the best solution is to sideload your answers record so that it appears alongside the questions record in your JSON root. 也许最好的解决方案是加载您的答案记录,使其与JSON根中的问题记录一起显示。 This is of course assuming that you have control over your JSON format, so this answer does not apply if this is not the case. 当然,这是假定您可以控制JSON格式,因此,如果不是这样,则此答案不适用。 If you are using active-model-serializers, this is done via the options embed: :id and include: true . 如果您使用的是active-model-serializers,则可以通过embed: :idinclude: true选项来完成。 You would have a question_serializer.rb with something like 您将拥有一个类似于以下内容的question_serializer.rb

class QuestionSerializer < ActiveModel::Serializer
  attributes :id, :title
  has_many :answers, embed: :id, include: true
end

And also an answer_serializer.rb with something like 还有一个answer_serializer.rb与类似

class AnswerSerializer < ActiveModel::Serializer
  attributes :id, :title, :wynikid
end

I believe it should then work. 我相信这样就可以了。 I was having an issue with this just yesterday and posted a stackoverflow question on it here , but I figured out my issue earlier today and answered my own question. 我昨天才遇到这个问题,并在这里发布了一个stackoverflow问题,但是今天早些时候我想出了问题并回答了我自己的问题。 I was able to get a printout of my JSON in basically the same way you are trying to do with your handlebars code! 我能够以与尝试使用车把代码基本相同的方式获得JSON的打印输出!

Edit: grammar. 编辑:语法。

latest ember does not support embedded records out of box. 最新的余烬不支持嵌入式记录。 but you can parse embedded by hand 但您可以手动解析嵌入

http://emberjs.jsbin.com/AkovAPu/4 - example (will work if localhost:8888/questions return your json - { "questions": [...) http://emberjs.jsbin.com/AkovAPu/4-示例(如果localhost:8888 / questions返回json,则可以使用-{“ questions”:[...)

http://emberjs.jsbin.com/AkovAPu/4/edit - source (look at extractArray) http://emberjs.jsbin.com/AkovAPu/4/edit-来源(请看extractArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM