Ember.js REST Adapter without JSON root

后端 未结 5 1297
Happy的楠姐
Happy的楠姐 2020-12-06 18:56

The Ember.js REST Adapter expects the JSON to be returned as:

{
    \"person\": {
        \"first_name\": \"Barack\",
        \"last_name\": \"Obama\",
              


        
相关标签:
5条回答
  • 2020-12-06 19:17

    I solved this by extending DS.RESTSerializer. extractArray method needs to be overloaded when server response is array type.

    App.PersonSerializer = DS.RESTSerializer.extend({
        extractSingle: function (store, type, payload, id) {
            var wrappedObj = {};
            wrappedObj[type.typeKey] = payload;
            return this._super(store, type, wrappedObj, id);
        }});
    
    0 讨论(0)
  • 2020-12-06 19:20

    You could also normalize it into something ember would expect.

    App.PersonSerializer = DS.RESTSerializer.extend({
      normalizePayload: function(type, payload) {
        var typeKey = type.typeKey;
        return {
          typeKey: payload
        }
      }
    });
    
    0 讨论(0)
  • 2020-12-06 19:22

    I tried to add root to json. This works for me:

    App.Adapter = DS.RESTAdapter.extend({
        findAll: function(store, type, since) {
            var root, adapter;
                var plural; // Insert this line
            root = this.rootForType(type);
            adapter = this;
                plural = this.pluralize(root); // Insert this line
            return this.ajax(
                    this.buildURL(root), 
                    "GET",
                    {
                        data: this.sinceQuery(since)
                    }).then(function(json) {
                        eval ( "json = {" + plural + " : json }" ); // Insert this line
                        adapter.didFindAll(store, type, json);
                    }).then(null, DS.rejectionHandler);
        }
    });
    
    0 讨论(0)
  • 2020-12-06 19:24

    Yes, you can write your own custom REST adapter. Take a look at the source code in the JSONSerializer, RESTSerializer (which extends the JSONSerializer), and the REST adapter.

    Basically, the you need to override the extract* methods from the JSONSerializer.

    Currently, it looks something like this:

    extract: function(loader, json, type, record) {
      var root = this.rootForType(type);
    
      this.sideload(loader, type, json, root);
      this.extractMeta(loader, type, json);
    
      if (json[root]) {
        if (record) { loader.updateId(record, json[root]); }
        this.extractRecordRepresentation(loader, type, json[root]);
      }
    },
    

    Notice how it checks json[root] -- you'd have to write your custom method based on your expected API response.

    Another approach would be to "preprocess" the json from the API to use a root element. You could do this by finding out what methods call extract* (which passes it the json) and before it does so, modify the json to contain the root element.

    Hope this helps, please let me know if it's unclear.

    0 讨论(0)
  • 2020-12-06 19:31

    The easiest way is to not use the RESTSerializer but the much simpler JSONSerializer, which does not expect a root element.

    Good resources on understanding which serializer to use for a given API can be found in these two blog posts:

    http://thejsguy.com/2015/12/05/which-ember-data-serializer-should-i-use.html
    http://emberigniter.com/fit-any-backend-into-ember-custom-adapters-serializers/

    0 讨论(0)
提交回复
热议问题