简体   繁体   English

通过API端点加载原始数据,让Ember将其转换为相应的模型

[英]Load raw data via API endpoint, get Ember to transform it into corresponding models

I'd like Ember (and Data) to request models from an API endpoint, but transform the raw data into models so that Ember can use it transparently. 我希望Ember(和Data)从API端点请求模型,但是将原始数据转换为模型,以便Ember可以透明地使用它。

The problem 问题

We load in data via a number of API endpoints when the page loads. 页面加载时,我们通过许多API端点加载数据。 We also push data to the page via Pusher. 我们还通过Pusher将数据推送到页面。 In an effort to consolidate the transforming of raw data into a single location we've decided to put that logic on the client. 为了将原始数据的转换整合到一个位置,我们决定将这种逻辑放在客户端上。 When we push new data to the page, we have a utility method that transforms that data into an Interaction record. 当我们将新数据推送到页面时,我们有一个实用程序方法将数据转换为交互记录。

Now, we need to also transform the raw data that the server passes to the page via AJAX request. 现在,我们还需要转换服务器通过AJAX请求传递到页面的原始数据。 It needs to pass through the same utility method, but I'm not sure where to intercept the response from the API so that I can properly transform the data. 它需要通过相同的实用程序方法,但是我不确定在哪里拦截来自API的响应,以便我可以正确地转换数据。 Where should I be looking, and what methods are at my disposal? 我应该在哪里看,我可以使用哪些方法?

The raw data will be different based on the type of information it is, but here's a single example: 原始数据将根据其信息类型而有所不同,但这是一个示例:

{
    "created_at": "Wed May 01 18:32:43 +0000 2013",
    "id": 329664697345384448,
    "id_str": "329664697345384448",
    "text": "@steveWINton true dat",
    "user": {
        "created_at": "Thu Feb 14 13:07:51 +0000 2008",
        "id": 13470142,
        "id_str": "13470142",
        "name": "uo\u0287u\u0131\u028d\u01dd\u028c\u01dd\u0287s",
        "screen_name": "NOTsteveWINton"
    }
}

And it needs to be converted into the following format: 并且需要将其转换为以下格式:

Social.Interaction = DS.Model.extend({
    native_id: DS.attr("string"),
    screen_name: DS.attr("string"),
    name: DS.attr("string"),
    text: DS.attr("string")
});

When the page loads, we're passing in a list of interaction IDs. 页面加载后,我们将传递一个交互ID列表。 Ember will then sideload the full data by hitting the interactions API endpoint. 然后,Ember将通过点击交互API端点来侧载全部数据。 That part will not change from the way it currently works. 该部分不会改变它的当前工作方式。

What we want is to be able to pass raw data to the page from the interactions endpoint rather than the "final" version of the model. 我们想要的是能够将原始数据从交互端点传递到页面,而不是模型的“最终”版本。

just had surgery, so i am short answer guy right now. 刚刚做过手术,所以我现在很简短。 You'll want to create a custom serializer, possibly per type. 您可能想创建一个自定义序列化器,可能是每种类型。

See: https://github.com/emberjs/data/blob/master/TRANSITION.md 参见: https : //github.com/emberjs/data/blob/master/TRANSITION.md

Could you simply poll for this using vanilla $.ajax and do a push after you finish the "transform" you mention above? 您能否简单地使用香草$ .ajax对此进行轮询,并在完成上面提到的“转换”后进行推送?

var json = do_something_to_transform_it_here();
store.serializerFor('interaction').pushSinglePayload(store, 'interaction', json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM