Extend existing "meta" solution for JSON REST responses #815

Merged
merged 1 commit into from Apr 11, 2013

Projects

None yet

4 participants

@ghost
ghost commented Mar 17, 2013

This change allows custom meta properties returned in JSON responses to be passed to the store by extending the "meta" solution provided in 1787bff.

Meta properties are required to exist in the serializer configuration in order for them to be passed to the store.

serializer.configure({
    meta: 'meta',
    since: 'since',
    pagination: 'pages'
});

payload = {
    meta: {
        since: '123',
        pagination: { total_pages: 2 }
    },
    { results: [...] }
};

Configured properties will be added to the store typeMap.metadata for the appropriate type, under the keys defined in the serializer configuration, eg. store.typeMapFor(type).metadata.pagination would be set to the content of the pages property of the JSON response.

This affords 1 set of meta properties to be set (and updated) per model type in the store. The meta properties can be accessed in the controller via this.get('content.store.typeMaps.metadata')

@ghost Unknown referenced this pull request Mar 17, 2013
Closed

Adapter Should Allow For Meta-Results #124

Contributor

I think this is a great addition. I am currently customizing an adapter for the stackexchange api. All of their meta attributes are un-nested sitting at the root of the json response. Its not the happy path you get from AMS and this would help a ton. Example:

{
  "total": 18416451,
  "page_size": 10,
  "page": 1,
  "type": "comment",
  "items": [
    {}, {}, {}, {}
  ],
  "quota_remaining": 9957,
  "quota_max": 10000,
  "backoff": 10,
  "has_more": true
}
@ghost
ghost commented Mar 18, 2013

This comment is now void (see my later comment and commit everwakeful/data@08a9fd6), but I will leav it here for historical purposes.

@CodeOfficer Just for clarity, with what I have proposed, you would still have to customise your adapter. Any meta properties are still expected to be nested under the meta property of the JSON response (I have updated the description above to clarify that). This is due to the way sideload currently skips the meta property when loading data.

However, I suppose that if what you face is a common concern, it should be easy enough to ensure that sideload skips any properties in the serializer configuration. Thoughts?

Contributor

Ah bummer, I misunderstood. I'm looking at two 3rd party APIs this week for StackExchange and Flickr. Both of them define their meta as I have described above. I'd love to be able to handle multiple meta attributes. Having them all under one meta property is icing on the cake, but I don't think that's very realistic when dealing with 3rd party stuff.

@ghost
ghost commented Mar 18, 2013

@CodeOfficer Refactored to include your use case. You make a fair point and we should not assume that all api's will return data structured in the way we would like.

Contributor

That's love right there. Thank you @everwakeful for putting this together.

Yes indeed! Thank you @everwakeful for this.

@ghost
ghost commented Mar 18, 2013

@CodeOfficer @jimdalbano Rebased changes in master and cleaned up commits/code.

Owner
igorT commented Apr 8, 2013

Could you rebase this against master please? Also @tomdale can you comment on this?

@igorT igorT commented on the diff Apr 11, 2013
packages/ember-data/lib/serializers/json_serializer.js
@@ -13,6 +13,9 @@ DS.JSONSerializer = DS.Serializer.extend({
this.set('transforms', DS.JSONTransforms);
}
+ this.sideloadMapping = Ember.Map.create();
igorT
igorT Apr 11, 2013 Owner

You don't seem to be using this?

@ghost
ghost Apr 11, 2013

No, I specifically did not want to attempt to side load any of the meta properties if they were defined in the config as they would not have a related model

Owner
igorT commented Apr 11, 2013

Thanks, this seems super useful.

@igorT igorT merged commit 44f8de1 into emberjs:master Apr 11, 2013

1 check failed

default The Travis build failed
Details

@everwakeful @igorT I can't get it work.

// store.js
App.Adapter = DS.RESTAdapter.reopen({
  serializer: DS.RESTSerializer.create({
    meta: 'meta',
    maxId: 'max_id'
  }),

// Console
App.store.typeMapFor(App.Post).metadata;
Object {since: "51ac5b8f0eae9c764f000005"}

Any ideas?

Sorry. Found the problem

This is how it worked with me

var serializer = DS.RESTSerializer.create();

App.Adapter = DS.RESTAdapter.reopen({
  serializer: serializer,
});

serializer.configure({
  since_id: 'since_id',
  max_id: 'max_id'
});

App.Store = DS.Store.extend({
  adapter: App.Adapter.create()
});
@heartsentwined heartsentwined referenced this pull request in getoutreach/epf Aug 13, 2013
Closed

metadata support #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment