JSONSerializer:未调用overriden方法

时间:2014-05-20 19:19:43

标签: json serialization ember.js ember-data

使用Ember.js& Ember Data,我这样定制DS.JSONSerializer

DS.JSONSerializer.reopen({
    extractArray: function (store, type, payload) {
        for (var x = 0; x < payload.length; x++) {
            payload[x] = this.normalize(type, payload[x]);
            store.push(type, payload[x]);
        };
        return payload;
    },
    extractSingle: function (store, type, payload, id, requestType) {
        var record = this.normalize(type, payload);
        store.push(type, record);
        return record;
    },
    serialize: function(record, options) {

        var json = {};

        if (options && options.includeId) {
            var id = get(record, 'id');

            if (id) {
                json[get(this, 'primaryKey')] = id;
            }
        }

        record.eachAttribute(function (key, attribute) {
            this.serializeAttribute(record, json, key, attribute);
        }, this);

        record.eachRelationship(function (key, relationship) {
            if (relationship.kind === 'belongsTo') {
                this.serializeBelongsTo(record, json, relationship);
            } else if (relationship.kind === 'hasMany') {
                this.serializeHasMany(record, json, relationship);
            }
        }, this);

        return json;
    },
    serializeHasMany: function (record, json, relationship) {
        var hasManyRecords, key;
        key = relationship.key;
        hasManyRecords = Ember.get(record, key);
        if (hasManyRecords) {
            json[key] = [];
            hasManyRecords.forEach(function (item, index) {
                // use includeId: true if you want the id of each model on the hasMany relationship
                json[key].push(item.serialize({ includeId: true }));
            });
        } else {
            this._super(record, json, relationship);
        }
    },
    serializeBelongsTo: function (record, json, relationship) {
        var belongsTo, key;
        key = relationship.key;
        belongsTo = Ember.get(record, key);
        if (belongsTo) {
            json[key] = [];
            belongsTo.forEach(function (item, index) {
                // use includeId: true if you want the id of each model on the hasMany relationship
                json[key].push(item.serialize({ includeId: true }));
            });
        } else {
            this._super(record, json, relationship);
        }
    }
});

我尝试调试某些错误,因此我在serializeserializeHasMany上设置了断点。 Ember怎么可能没有调用这些方法?还有什么方法可以调用?

1 个答案:

答案 0 :(得分:1)

reopen用于向实例添加属性/方法。 reopenClass用于向类添加属性/方法。忽略这两件事,你真的不应该重新打开默认实现,而应该将你的应用程序适配器设置为它的扩展。

App.ApplicationSerializer = DS.JSONSerializer.extend({
  extractArray: function (store, type, payload) {
        for (var x = 0; x < payload.length; x++) {
            payload[x] = this.normalize(type, payload[x]);
            store.push(type, payload[x]);
        };
....
})

这里有一个简短的示例,它可以使用其他适配器,但它应该是相同的概念。此外,我不确定您如何设置使用该序列化程序,这也可能是您的问题。快速浏览一下转换文档,获取一些额外的帮助:https://github.com/emberjs/data/blob/master/TRANSITION.md http://emberjs.jsbin.com/OxIDiVU/499/edit