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.

The problem

We load in data via a number of API endpoints when the page loads. We also push data to the page via 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. 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. 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")
});

cc-ing @toranb in case this might directly involve routing through the Django Rest Adapter.

I found another response that seemed to indicate that Ember had a preprocessData method but when I tried to implement that it did nothing:

Just to clarify -you want to override the generic “side loading” ember-data does when your $.ajax request is resolved successfully? (during normal find / find all like logic) ? -what about create /update /delete?

When the page loads, we’re passing in a list of interaction IDs. Ember will then sideload the full data by hitting the interactions API endpoint. 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.