我正在尝试修改RESTSerializer中pushPayload的行为。但是从不调用该函数。
pushPayload: function(store, payload) {
console.log('pushPayload');
payload = this.normalizePayload(payload);
for (var prop in payload) {
var typeName = this.typeForRoot(prop),
type = store.modelFor(typeName),
typeSerializer = store.serializerFor(type);
/*jshint loopfunc:true*/
var normalizedArray = map.call(Ember.makeArray(payload[prop]), function(hash) {
return typeSerializer.normalize(type, hash, prop);
}, this);
store.pushMany(typeName, normalizedArray);
}
},
normalizePayload: function(payload) {
console.log('normalize');
return payload;
},
这将仅输出:
正常化
我真的不知道发生了什么。我从the master两种方法复制了。没有拼写错误,如果调用normalizePayload也应该调用pushPayload!
答案 0 :(得分:1)
normalizePayload
来自extractSingle
store.find('foo', 1);
),store.extractArray
(store.find('foo');
)和pushPayload
(store.pushPayload('foo', obj);
)
您需要在商店中实际调用pushPayload
并定义序列化程序的类型。另外要小心,map在文档的前面定义:
var map = Ember.ArrayPolyfills.map;
App.FooSerializer = DS.RESTSerializer.extend({
pushPayload: function(store, payload) {
console.log('pushPayload');
payload = this.normalizePayload(payload);
for (var prop in payload) {
var typeName = this.typeForRoot(prop),
type = store.modelFor(typeName),
typeSerializer = store.serializerFor(type);
/*jshint loopfunc:true*/
var normalizedArray = map.call(Ember.makeArray(payload[prop]), function(hash) {
return typeSerializer.normalize(type, hash, prop);
}, this);
store.pushMany(typeName, normalizedArray);
}
},
});