Serializer for custom json payload

Hi,

I’m new in the ember world and have some difficulties with where to put what. Maybe for someone it will be peace of cake.

Any way I have managed to create my first app, that loads json data form the server. However later on the structure of json has been changed and I cannot figure out how to solve that in ember models or in any other way.

I have json structure like:

{
  "users": [
      {
        "id":1,
        "title":"User 1",
      },
      ...
    ],
  "error":null
}

For Ember conventions everything is ok with users array, but what to do with error? Should I write a serializer? Or should error be defined as a model?

Thanks for any help.

I’m using:

Ember : 1.2.0
Ember-data : 0.14 Handlebars : 1.1.2 jQuery : 1.10.2

If you don’t want Ember Data to try to convert error into a model, then you can just remove it from the payload before that happens. I usually do that in ApplicationSerializer#extractMeta(store, type, payload).

Whatever serializer handles serializing Users model.

App.UserSerializer = DS.RESTSerializer({
  extractMeta: function(store, type, payload) {
    if ( payload.error ) {
      // handle the error somehow
    }
    delete payload.error;
    return payload;
  }
});

Thanks for replay. The idea suits me, but I do not know if that is working. But that I think is my poor knowledge of ember.

Where should the model’s serializer be defined? It is hard to find working example of serializers. Ive structured my ember app as ember-rails generator did. And my user model:

App.User = DS.Model.extend({
  title: DS.attr('string'),
});

My store.js file is:

App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.RESTAdapter.create({
    namespace: 'api/v1'
  })
});

Thanks.

It looks like you’re using an old version of Ember Data. There is a document that describes the difference between the latest beta and the old version. I’m not sure how you would do this in the old version.

What guides are you following?

I’ve used few different tutorials and tried to keep code as similar as http://emberjs.com/guides/. But to have global picture I have used some tutorials that are 3-6 months old. And that I see is the main problem.

Maybe you can suggest some guides that could lead me to more info? I think I will start the app from scratch while it is still in early stages. Because ember data is very promising.

The Ember Models section in Ember Guides is the best collection of resources on the current state of Ember Data. For more succinct reference, you should look at the Ember Data betas slideshow by Yehuda Katz. TRANSITIONS.md is probably the most comprehensive summary of how to use Ember Data.

What are you using as the backend?

For backend I am using Ruby on rails. However api is already used in other apps and thats way I need to look for modifications only from app side.

I did not pay big attention to transitions.md and there is a lot info.

Its time for reading, then.

Thanks.

Yeah, it’s easy to overlook the value of that document.

Make sure that you’re using the latest version of Ember Data. Look in the console to make sure that your version shows that its a beta.

It should be something like DEBUG: Ember Data : 1.0.0-beta.3

For the record the actual code should be:

App.UserSerializer = DS.RESTSerializer.extend({
  extractMeta: function(store, type, payload) {
    if ( payload.error ) {
      // handle the error somehow
    }
    delete payload.error;
    return payload;
  }
});

So in the end I’ve created the app from scratch with most updated libs and everything connects and works. Now i’m using

DEBUG: Ember : 1.2.0 DEBUG: Ember Data : 1.0.0-beta.2 DEBUG: Handlebars : 1.1.2 DEBUG: jQuery : 1.10.2

Ember data in file shows // Version: v1.0.0-beta.3 For this time I have been following only official guides it seems it leads me to right code.

Thanks a lot for the right direction.