规范化normalizePayload中的ember-data有效负载,但“type”为null

时间:2014-01-05 16:27:03

标签: ember.js ember-data

我正在尝试编写代码以对嵌入式记录进行一般规范化。我正在尝试在序列化程序的“normalizePayload”方法中执行此操作,因为“extractObject”和“extractArray”不会在store.pushPayload调用上调用(我使用pushPayload预加载数据)。

这是我的代码:

App.ApplicationSerializer = DS.ActiveModelSerializer.reopen
    embeddedRelationships: []

    extractHasOne: (payload, relationship_name) ->
        object = payload[relationship_name]
        objectId = object['id']
        payload["#{relationship_name}_id"] = objectId
        delete payload[relationship_name]

        return object

    extractHasMany: (payload, relationship_name) ->
        objects = payload[relationship_name]
        objectIds = objects.mapProperty('id')
        payload["#{Ember.Inflector.inflector.singularize(relationship_name)}_ids"] = objectIds
        delete payload[relationship_name]

        return objects

    extractRelationships: (payload) ->
        extracted_objects = {}

        this.embeddedRelationships.forEach((relationship_name) ->
            relationship_payload = payload[relationship_name]
            if relationship_payload instanceof Object
                # has one
                extracted_objects[relationship_name] = this.extractHasOne(relationship_payload, relationship_name)
            else if relationship_payload instanceof Array
                # has many
                extracted_objects[relationship_name] = this.extractHasMany(relationship_payload, relationship_name)
        , this)

        return extracted_objects

    normalizePayload: (type, payload) ->
        if payload[type]?
            # single object
            this.extractRelationships(payload[type])
        else if payload[Ember.Inflector.inflector.pluralize(type)]?
            # many objects
            payload[Ember.Inflector.inflector.pluralize(type)].forEach((object_payload) ->
                this.extractRelationships(object_payload)
            , this)

注意:这还没有完成,因为我没有合并所有提取的对象并将它们侧载。

在任何一种情况下,我的想法是在子类中,我用一个字符串列表覆盖“嵌入式关系”,这些字符串表示必须为该模型规范化的关系。

我遇到的问题很简单:使用null“type”值调用“normalizePayload”。

我正在预加载数据:

this.store.pushPayload('category', window.preloadCategories)

这是一个蠢货,还是我错过了什么?

1 个答案:

答案 0 :(得分:3)

pushPayload接受serializer的类型和抓取,然后在自定义序列化程序上调用pushPayload,它不会占用类型,因此不会将任何类型传递给normalizePayload 1}}。

我建议为Category而不是Application创建自定义序列化程序,然后您就会知道它的类别类型。

背后的原因是pushPayload适合推送多种类型(又名有效载荷不仅仅是CategoryPost

商店

pushPayload

  /**
    Push some raw data into the store.

    The data will be automatically deserialized using the
    serializer for the `type` param.

    This method can be used both to push in brand new
    records, as well as to update existing records.

    You can push in more than one type of object at once.
    All objects should be in the format expected by the
    serializer.

    ```js
    App.ApplicationSerializer = DS.ActiveModelSerializer;

    var pushData = {
      posts: [
        {id: 1, post_title: "Great post", comment_ids: [2]}
      ],
      comments: [
        {id: 2, comment_body: "Insightful comment"}
      ]
    }

    store.pushPayload('post', pushData);
    ```

    @method pushPayload
    @param {String} type
    @param {Object} payload
  */

  pushPayload: function (type, payload) {
    var serializer;
    if (!payload) {
      payload = type;
      serializer = defaultSerializer(this.container);
      Ember.assert("You cannot use `store#pushPayload` without a type unless your default serializer defines `pushPayload`", serializer.pushPayload);
    } else {
      serializer = this.serializerFor(type);
    }
    serializer.pushPayload(this, payload);
  },

串行器上的pushPayload

    It will first normalize the payload, so you can use this to push
    in data streaming in from your server structured the same way
    that fetches and saves are structured.

    @method pushPayload
    @param {DS.Store} store
    @param {Object} payload
  */
  pushPayload: function(store, payload) {
    payload = this.normalizePayload(null, payload);

    for (var prop in payload) {
      var typeName = this.typeForRoot(prop),
          type = store.modelFor(typeName);

      /*jshint loopfunc:true*/
      var normalizedArray = map.call(payload[prop], function(hash) {
        return this.normalize(type, hash, prop);
      }, this);

      store.pushMany(typeName, normalizedArray);
    }
  },