Javascript 如何在Ember.js模板中显示多条记录?

Javascript 如何在Ember.js模板中显示多条记录?,javascript,json,model,ember.js,ember-data,Javascript,Json,Model,Ember.js,Ember Data,我对模型的定义如下: 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' ) }); { "questions":

我对模型的定义如下:

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' ) 
});
{
    "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
                }
            ]
        }
    ]
}
O której wstajesz?
 O godzinie 6
 O godzinie 7
 ...
Kot czy kominiarz?
 ...
并从RESTAdapter获取数据,返回如下数据:

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' ) 
});
{
    "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
                }
            ]
        }
    ]
}
O której wstajesz?
 O godzinie 6
 O godzinie 7
 ...
Kot czy kominiarz?
 ...
现在我想完成的是显示如下列表:

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' ) 
});
{
    "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
                }
            ]
        }
    ]
}
O której wstajesz?
 O godzinie 6
 O godzinie 7
 ...
Kot czy kominiarz?
 ...
我尝试在Ember.js模板中使用此选项:

 {{#each controller}}

  {{title}}<br>

    {{#each answer in answers}}

    \t{{title}}<br>

    {{/each}}

  {{/each}}
{{#每个控制器}
{{title}}
{{{#答案中的每个答案} \t{{title}}
{{/每个}} {{/每个}}
显示问题,但不显示答案。控制台中没有任何错误,JSONLint也没有发现JSON有任何问题

如何让它工作

我使用:

把手1.0.0.0.js 余烬-1.0.0.0.min.js
余烬数据:v0.13-238-gbf84978

最新版本的余烬/余烬数据不支持嵌入式记录。这是有文档记录的,但我发现解决方法非常笨拙,我自己无法让它工作

也许最好的解决方案是将答案记录侧向加载,以便它与JSON根目录中的问题记录一起显示。当然,这是假设您可以控制JSON格式,因此如果情况并非如此,则此答案不适用。如果您使用的是活动模型序列化程序,则可以通过选项
embed::id
include:true
来完成。您将有一个
question\u serializer.rb
,内容如下

class QuestionSerializer < ActiveModel::Serializer
  attributes :id, :title
  has_many :answers, embed: :id, include: true
end
class AnswerSerializer < ActiveModel::Serializer
  attributes :id, :title, :wynikid
end
我相信它应该会起作用。就在昨天,我对此有一个问题,并在上面发布了一个stackoverflow问题,但今天早些时候我发现了我的问题,并回答了我自己的问题。我能够得到我的JSON的打印输出,基本上与您尝试使用Handlebar代码的方式相同


编辑:语法。

最新的余烬不支持开箱即用的嵌入式记录。但是你可以手工解析嵌入的

-示例(如果localhost:8888/questions返回您的json-{“questions”:[…]将起作用)

-源(查看提取阵列)