codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Ember.js REST adapter handling different JSON structure


By : , Category : javascript

Assuming you are ok with writing your own adapter to deal with the difference, in the success callback you can simply modify the incoming name from "data" to your specific entity -in the case above "messages"

I do something like this to give you and idea of what if possible in a custom adapter

In the link below I highlighted the return line from my findMany

The json coming back from my REST api looks like

[
    {
        "id": 1,
        "score": 2,
        "feedback": "abc",
        "session": 1
    },
    {
        "id": 2,
        "score": 4,
        "feedback": "def",
        "session": 1
    }
]

I need to transform this before ember-data gets it to look like this

{
    "sessions": [
        {
            "id": 1,
            "score": 2,
            "feedback": "abc",
            "session": 1
        },
        {
            "id": 2,
            "score": 4,
            "feedback": "def",
            "session": 1
        }
    ]
}

https://github.com/toranb/ember-data-django-rest-adapter/blob/master/packages/ember-data-django-rest-adapter/lib/adapter.js#L56-57

findMany: function(store, type, ids, parent) {
    var json = {}
    , adapter = this
    , root = this.rootForType(type)
    , plural = this.pluralize(root)
    , ids = this.serializeIds(ids)
    , url = this.buildFindManyUrlWithParent(store, type, ids, parent);

    return this.ajax(url, "GET", {
      data: {ids: ids}
    }).then(function(pre_json) {
      json[plural] = pre_json; //change the JSON before ember-data gets it
      adapter.didFindMany(store, type, json);
    }).then(null, rejectionHandler);
},
ReLated :

You need to extend your adapter to handle the errors, the REST Adapter does NOT do this for you (only the Active Model one)

Something like this:

App.ApplicationAdapter = DS.RESTAdapter.extend({

  ajaxError: function(jqXHR) {
    var error = this._super(jqXHR);
    if (jqXHR && jqXHR.status === 422) {
      var response = Ember.$.parseJSON(jqXHR.responseText),
          errors = {};

      if (response.errors !== undefined) {
        var jsonErrors = response.errors;
        Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) {
          errors[Ember.String.camelize(key)] = jsonErrors[key];
        });
      }
      return new DS.InvalidError(errors);
    } else {
      return error;
    }
  }
});

Override serializeIntoHash in the serializer:

serializeIntoHash: function(data, type, record, options) {
    Ember.merge(data, this.serialize(record, options));
}

For your second question, look at typeForRoot. Why not just read the entire page on adapters?

My best guess is, you need to update your error serializer to send data in a format expected by Ember Data JSON Adapter.

It's best explained in detail in this stackoverflow answer, but as a gist you should format your data in a following way:

{
  "errors": 
  [
    {
      "detail": "is invalid",
      "source": {
        "pointer": "data/attributes/phone"
      }
    }
  ]
}  

The pointer must correspond to your model attribute, so phone in this example.

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.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .