pushPayload没有被调用

时间:2014-06-25 03:57:25

标签: ember.js ember-data

我正在尝试修改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!

1 个答案:

答案 0 :(得分:1)

{p} normalizePayload来自extractSingle store.find('foo', 1);),store.extractArraystore.find('foo');)和pushPayloadstore.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);
    }
  },
});

http://emberjs.jsbin.com/OxIDiVU/730/edit