Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter Should Allow For Meta-Results #124

Closed
ghempton opened this issue Feb 27, 2012 · 12 comments
Closed

Adapter Should Allow For Meta-Results #124

ghempton opened this issue Feb 27, 2012 · 12 comments
Labels
🏷️ feat This PR introduces a new feature
Milestone

Comments

@ghempton
Copy link
Member

Currently, RestAdapter expects everything in the returned JSON to correspond to model data. This is even more strictly enforced with the introduction of side-loading.

An extremely common scenario, however, is for the server to return metadata associated with the query, e.g. aggregate counts, pagination info, etc. The ability to capture this information is also the first step for features such as pagination.

@wycats
Copy link
Member

wycats commented Feb 28, 2012

How would you feel about loading metadata in a meta property?

@ghempton
Copy link
Member Author

I am fine with that.

As far as storing the meta information is concerned, I currently attach it to the model array itself. Because I am trying to keep all this inside the adapter, this restricts metadata to the findQuery method as I am not given access to the model array inside findAll.

Another thought I have is that there might be some utility in allowing custom model array implementations: e.g. PaginatedModelArray etc.

@wagenet
Copy link
Member

wagenet commented Nov 2, 2012

@ghempton Seems like you could do this in your adapter. If you need some hooks could you submit a PR?

@wagenet wagenet closed this as completed Nov 2, 2012
@rlivsey
Copy link

rlivsey commented Jan 22, 2013

What's the current thinking on this?

I'm currently doing as @ghempton mentioned and overriding didFindQuery in my adapter and attaching the meta to the recordArray but this feels wrong somehow.

There appears to be some support for the meta property now in the serializers since 1787bff, that commit mentions pagination as a use-case but I can't see how that would be implemented. extractMeta on the serializer doesn't have access to the records and so can't do anything useful with that data as far as I can make out.

@wildchild
Copy link

+1. Metadata makes sense. Guys, have you got a temp workaround?

@christophermanning
Copy link

When I attach meta like https://github.com/rails-api/active_model_serializers#attributes suggests, I don't see a way to access that data without a custom serializer since https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L193-L198 doesn't make it accessible. Having the meta data available in a meta property would be great.

@mbrennan-afa
Copy link

👍 - Can we please have this re-evaluated.

@brennanmceachran
Copy link

Currently facing @christophermanning issue as well...

edit: Gotten around it by overwriting didFindQuery in my adapter:

    didFindQuery: function(store, type, payload, recordArray) {
        var loader = DS.loaderFor(store);

        loader.populateArray = function(data) {
            recordArray.load(data);

            // This adds the meta property returned from the server
            // onto the recordArray sent back
            recordArray.set('meta', payload.meta);
        };

        this.get('serializer').extractMany(loader, payload, type);
    }

@ghost
Copy link

ghost commented Mar 17, 2013

I have submitted a PR (#815) which extends the functionality of the "meta" solution in 1787bff. It allows the use of custom meta properties, by passing them to the store typeMap.

@josepjaume
Copy link

I think the use case where you want to store metainformation of a query is pretty common and worth looking into it.

The solution @christophermanning is proposing sounds clean enough; why not have it implemented on the Adapter by default?

@sly7-7
Copy link
Contributor

sly7-7 commented Apr 16, 2013

Since the #815 has been merged, does it solve it as well ?

@ghost
Copy link

ghost commented Apr 18, 2013

The issue I had with the recordArray solution above is that if you execute a store.filter which calls findQuery, the recordArray returned does not contain the meta property. This is because what is returned is in fact a filteredRecordArray rather than the adapterPopulatedRecordArray returned by findQuery (where the meta property is set). Added to that (as mentioned above) this restricts any call that requires metadata to findQuery.

If you take a look at #815, it should solve these issues (it was specifically designed for this, as I had a need to send pagination data back from the server). There is a quick explanation of how it works with the ticket, but I am happy to create more in-depth examples if someone can suggest where I should place them, as we are yet to have ember-data docs.

@runspired runspired added 🏷️ feat This PR introduces a new feature and removed Improvement labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ feat This PR introduces a new feature
Projects
None yet
Development

No branches or pull requests