Parse.Collection.fetch不考虑"parse: true"选项



我有一个模型,我想在其中添加一个解析方法,以便完成额外的数据工作(为日期字段设置一个moment.js对象)。

但函数从未被调用(模型和集合)。

集合:

class SomethingCollection extends Parse.Collection
    model: SomethingModel
    parse: ->
        console.log('parse from collection')

型号:

class SomethingModel extends Parse.Object
    className: 'Something'
    parse: ->
        console.log('parse from model')

从一个角度来看:

@collection = new SomethingCollection()
@listenTo( @collection, 'add', -> console.log('fire add event') )
@collection.fetch(parse: true, silent: false, add: true)

编辑:

这似乎发生在Parse.Query.find回调中,请参阅下面的代码注释。所以它也不能在initialize方法中完成,但在其他地方呢?我怀疑Parse.Object与Bakbone不太相似。模型

find: function(options) {
  var self = this;
  options = options || {};
  var request = Parse._request({
    route: "classes",
    className: this.className,
    method: "GET",
    useMasterKey: options.useMasterKey,
    data: this.toJSON()
  });
  return request.then(function(response) {
    return _.map(response.results, function(json) {
      var obj;
      if (response.className) {
        obj = new Parse.Object(response.className); // <- no attributes or options used, blank object
      } else {
        obj = new self.objectClass(); // <- no attributes or options used, blank object
      }
      obj._finishFetch(json, true); // <- magically do things out of any constructor or parse function
      return obj;
    });
  })._thenRunCallbacks(options);
},

我没有找到其他方法,只能重新声明诅咒的_finishFetch方法:

original_finishFetch = _(Parse.Object.prototype._finishFetch)
Parse.Object.prototype._finishFetch = (serverData, hasData) ->
  original_finishFetch.bind(@)(@parse(serverData), hasData)

通过这种方式,数据在解析方法中进行处理,就像任何骨干模型或实现骨干模型接口的任何sdk一样。

最新更新