ember js:#each显示子对象内容

时间:2014-11-29 22:59:29

标签: javascript ember.js ember-data

我在“sectionfeed”模型中将section作为belongsTo。我想在#each中打印该部分的内容,如下所示。它不起作用。

如何使用ember js中的#each打印子对象的内容

模板

{{#each sectionfeed in model.sectionfeeds}}
                        <tr>
                                <td> {{#link-to 'section' sectionfeed.section}}{{sectionfeed.id}}{{/link-to}} </td>
                                <td> {{sectionfeed.section.name}} </td> <-- Not getting printed
                                <td> {{fromNow sectionfeed.section.modifiedAt}} </td>
                                <td> {{fromNow sectionfeed.section.updatedAt}} </td>
                                <td> {{fromNow sectionfeed.section.liverpooledAt}} </td>
                            <td>
                                <button class="btn btn-primary btn-sm" {{action 'remove_section_feed' sectionfeed}}>Remove</button>
                            </td>
                        </tr>
                    {{ else }}
                        <tr>
                            <td colspan="5">
                                No Section found.
                            </td>
                        </tr>
                    {{/each}}

来自端点的响应

{"sectionfeed":{"id":"c9eb3c84-424c-4437-d9fa-1499373a8309","createdAt":1417225274651,"modifiedAt":1417225274651,"sectionDefinitionID":"0358b97a-b9ae-4c67-987d-03afffa4655e","feedDefinitionID":"0b8f8dd5-2b15-4fbc-9a49-bd91dfa62750","section":{"id":"0358b97a-b9ae-4c67-987d-03afffa4655e","createdAt":1416672390865,"modifiedAt":1417295499501,"appleTouchIconURL":null,"tintColor":null,"fontColor":null,"coverImageURL":null,"name":"Section to Feed Association Test ","channelDefinitionID":"7fd619e5-7633-4a5e-8c05-94d42c65945d","icon":null,"liverpooledAt":1417295499501,"sortOrder":0.0}},"meta":{}}

sectionfeed model

    /*global Ember*/
Backoffice.Sectionfeed = DS.Model.extend({
    sectionDefinitionID: DS.attr('string'),
    sections:DS.belongsTo('section', {async: false}),
    feedDefinitionID: DS.attr('string')
});
DS.RESTAdapter.reopen({
    ajaxOptions: function(url, type, hash) {
        hash = hash || {};
        if (type === 'PUT') {
            hash.data = hash.data || {};
            hash.data['_method'] = type;
            type = 'DELETE';
        }
        return this._super(url, type, hash);
    }
});

部分模型

/*global Ember*/
Backoffice.Section = DS.Model.extend({
    // Section
    name: DS.attr('string'),
    icon: DS.attr('string'),
    channelDefinitionID: DS.attr('string'),
    liverpooledAt: DS.attr('date'),

    // DataModel
    createdAt: DS.attr('date'),
    modifiedAt: DS.attr('date'),

    // Cover Item
    appleTouchIconURL: DS.attr('string'),
    coverImageURL: DS.attr('string'),
    tintColor: DS.attr('string'),
    tintColorStyle: function () {
        return 'background-color:' + this.get("tintColor") + ';';
    }.property('tintColor'),
    fontColor: DS.attr('string'),
    fontColorStyle: function () {
        return 'color:' + this.get("fontColor") + ';';
    }.property('fontColor')
});

// probably should be mixed-in...
Backoffice.Section.reopen({
    attributes: function () {
        var model = this;
        return Ember.keys(this.get('data')).map(function (key) {
            return Em.Object.create({ model: model, key: key, valueBinding: 'model.' + key });
        });
    }.property()
});

1 个答案:

答案 0 :(得分:0)

Ember-Data期望相关项目位于响应中的单独对象中。您需要实现嵌入式记录mixin才能实现此功能。 (http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html