如何使用基本适配器从Ember.js模型中显示模板中的嵌套数据?

时间:2013-05-02 03:56:00

标签: javascript ember.js ember-data

商店:

App.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.BasicAdapter.create()
});

型号:

var Profiles = DS.Model.extend({
    displayName: DS.attr('string'),
    profileName: DS.attr('string')
}),
User = DS.Model.extend({
    email: DS.attr('string'),
    phone: DS.attr('string'),
    profiles: DS.hasMany(Profiles, {embedded: true})
});

模型的JSON数据:

{
    "email": "some email",
    "phone": "some phone number",
    "profiles": [
        {
            "displayName": "some name",
            "profileName": "some name"
        },
        {
            "displayName": "some name",
            "profileName": "some name"
        }
    ]
}

在模板中:

{{#if model.isLoaded}}
    email: {{model.email}} // displayed
    {{model.profiles.length}} // number of objects in array is displayed

    {{#each model.profiles}}
        // this line generates error Uncaught TypeError: Cannot call method
           '_create' of undefined ember-data.js:2560
    {{/each}}
{{/if}}

知道为什么这条线会产生错误?特别是如果model.profiles.length显示正确的值?

1 个答案:

答案 0 :(得分:0)

首先,用于在用户中嵌入Profiles的语法不再相关。请检查this answer以获取正确的设置方法。