Post Model different than received model


#1

Hi, I have a requirement where in I have a REST API (Not controlled by me) which needs some JSON data in a specific format as a Post request and returns a JSON response in a different format. My question is, can Ember data be used here? Can I write custom Adapter which modifies the request when we make a find query I can modify the request from ‘get’ to ‘post’ and add the post data and in turn get the data using custom serializer.

Thanks.


#2

Hello @batman. Yes you can do this by creating a custom adapter. You may want to look at the http://emberjs.com/api/data/classes/DS.Adapter.html documentation but it sounds like you will want to create a custom find method for your adapter. This could be something as simple as

App.ApplicationAdapter = DS.RESTAdapter.extend({
    find: function(store, type, id) {
        var data = {
            id: id,
            // other JSON data here
        };
        return this.ajax(this.buildURL(type.typeKey, id), 'POST', data);
    }
});

Please let me know if you run into any trouble creating a custom adapter. I’m working on putting together a guide to cover custom adapters atm.


#3

@bmac Thank you for jumping in on this after catching it on #emberjs! The Ember community once again warms the cockles of my cold, hard, heart.


#4

Thanks for the response :). Bmac if you can share the guide for creating custom adapters that will be great. Thanks a bunch.


#5

The guide is available here http://emberjs.com/guides/models/customizing-adapters/

Prs ( https://github.com/emberjs/website/blob/master/source/guides/models/customizing-adapters.md ) and feedback welcome.


#6

Thanks a lot Bmac :smile: