Ember.js RestAdapter - 如何连接到外部API

时间:2014-03-22 22:03:33

标签: ember.js

如何使用ember.js的RestAdapter连接外部API(不在我的服务器域上)?

  1. 逐个连接到多个外部API,
  2. 获取JSON响应后,只选择JSON对象/属性的子集并将它们转换为标准模型(在我的ember.js应用程序中定义)?

1 个答案:

答案 0 :(得分:0)

假设您正在使用Ember数据。

您将很难为单个模型多次调用不同的端点,然后创建模型。我建议使用原始ajax,然后将数据加载到商店中。

以下是有关Ember Data的侧载的说明

  /**
    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
  */