Reputation: 8017
Working with Ember.js & Ember Data, I'm customizing the DS.JSONSerializer
like this:
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);
}
}
});
I'm attempting to debug some errors, so I placed breakpoints on serialize
and serializeHasMany
. How is it possible that Ember isn't calling these methods? And what methods are being called instead?
Upvotes: 0
Views: 240
Reputation: 47367
reopen
is meant for adding properties/methods to an instance. reopenClass
is meant for adding properties/methods to the class. Ignoring those two things, you really shouldn't reopen the default implementation, instead you should set your application adapter to an extension of it.
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]);
};
....
})
Here's a quickie example of it hitting my alert, it's using the rest adapter, but it should be the same concept. Additionally I'm not sure how you are setting up to use that serializer, that also might be your issue. Take a quick look at the transition document for some additional help: https://github.com/emberjs/data/blob/master/TRANSITION.md http://emberjs.jsbin.com/OxIDiVU/499/edit
Upvotes: 1