Comments (5)
I solved this thus
App.ApplicationSerializer = DS.DjangoRESTSerializer.extend
extractMeta: (store, type, payload)->
if payload && payload.results && payload.count
store.metaForType type, total: payload.count
extractArray: (store, type, payload)->
payload = if Ember.typeOf(payload) == 'array' then payload else payload.results
@_super store, type, payload
from ember-django-adapter.
Thanks for the pointer @bortevik. We're already setting count
as total
in extractMeta
:
https://github.com/dustinfarris/ember-django-adapter/blob/version-1.0/addon/serializers/drf.js#L25
We also set next
and previous
to the URLs from DRF but that's not useful. See the proposed solution below.
We also take care of results
in extractArray
:
https://github.com/dustinfarris/ember-django-adapter/blob/version-1.0/addon/serializers/drf.js#L68
This one is slightly different that what you do but it achieves the same result.
There is still an open question about how pagination should actually work with DRF from a high level. I just read the ember metadata docs (http://emberjs.com/guides/models/handling-metadata/) and it looks like they expect the server-side to implement a limit
query param. Since the page size is set in the server-side code with DRF, we'll have to come up with something else.
Here are two options I've come up:
- Set
next
andprevious
inextractMeta
to the query params that would generate the next and previous pages. Thenext
andprevious
metadata values would be accessed on the specific query withvar meta = result.get("content.meta");
It would also be possible to access the metadata on the type withvar meta = this.store.metadataFor("post");
, however, onlytotal
would be accurate there. The values ofnext
andprevious
in the type metadata wouldn't make sense. It might even be possible to remove thenext
andprevious
variables from the type metadata but I'm not sure.
2)Set a pageSize
on the type metadata and manage the next and previous pages manually in applications.
Both options tie the application to DRF which isn't ideal but it's hard to avoid. To me it feels like option 1 is better because we're using something that DRF already gives us.
@bortevik / @dustinfarris What do you think of these two options? Can you think of any other options?
from ember-django-adapter.
Regarding the limit
query param, is that an Ember requirement, or just an Ember suggestion? From that link you posted it looks like they just used it as an example. Either way, DRF already accepts a page_size
query parameter to dynamically adjust the page size, and it looks like we could modify that easily in settings.py
as described in the DRF docs if we had to:
REST_FRAMEWORK = {
'PAGINATE_BY_PARAM': 'limit',
...
If limit
is a requirement, we could add this setting example to the docs. (Speaking of which, we have been mentioning several things that should go in "the docs" but we don't really have any docs aside from the README. I'll open a separate issue to discuss this. edit: see #26)
Regarding the next/previous/count data, I like 'option 1', but I think the next/previous values are relevant even for the type metadata depending on the use-case (the user can decide whether to track pagination on the model or procedurally using the result). As a side note, I don't see why we need to change the name of "count" to "total" just because the example in the Ember docs uses "total". Is the "total" name actually required anywhere in Ember? If not, let's make a note to change that lineβand, again, to make a nice example in the docs.
I don't really like the format of the value DRF returns for next/previous (e.g. "?page=2"
). This isn't particularly useful for our needs. We could extract the numeric value, something like:
store.metaForType(type, {
next: parseInt(payload.next.slice(6)), // '?page=2' becomes 2
...
and then the rest is pretty straight-forward for the user:
...
var meta = this.store.metadataFor('post');
var moreResults = this.store.find('post', {
pageSize: 10,
page: meta.next
});
(do query params hit our keyForAttribute
method? if not, i guess that should be page_size: 10
, or we'll need to decide whether we can/should decamilize query param keys)
from ember-django-adapter.
The limit
query param and the total
metadata variable is an Ember suggestion; I guess it's the defaults from Rails which seems to be used a lot in the Ember documentation. My initial thought was to try to support the examples in the Ember docs. The idea was to try to make it so an Ember app is independent of the ED adapter being used. But I'm not so sure that we should work too hard for that goal. In the end it's probably not possible for somebody to change from a Rails to a Django back-end (or vice versa) by just swapping out the adapter.
For page_size
, I didn't know that DRF supported a dynamic page size so some of my thinking on this issue was misinformed.
I'll create a PR to change the pagination metadata to the DRF defaults (i.e. use count
), extract the numeric values for next
and previous
and add documentation for page_size
. I'll also add some integration tests to check if the query params are run through keyForAttribute
and write the tests and documentation appropriately.
from ember-django-adapter.
This is being implemented via PR #38.
from ember-django-adapter.
Related Issues (20)
- What are the advantages/disadvantages of using this package vs django-rest-framework-json-api? HOT 2
- Fastboot - Error: The adapter operation was aborted HOT 3
- upload file
- Why embedded record is not populated with Ember.Model? HOT 2
- An in-range update of loader.js is breaking the build π¨ HOT 9
- An in-range update of ember-data is breaking the build π¨ HOT 11
- An in-range update of ember-cli-htmlbars is breaking the build π¨ HOT 1
- Correctly serialize lists of URLs for nested relationships HOT 3
- An in-range update of ember-disable-prototype-extensions is breaking the build π¨ HOT 1
- An in-range update of ember-cli-inject-live-reload is breaking the build π¨ HOT 1
- Upgrade ember-cli
- Dependecy problem with ember-inflector HOT 1
- Update to Ember-Data v3.0 HOT 21
- Acceptance test fails because CRUD operation didn't finish up before app shutdown HOT 4
- Generate adapter and serializer in pods
- CircleCI fails with pip install mkdocs HOT 11
- Easy to get multiple versions of ember-data HOT 1
- Website/Documentation 404 HOT 8
- Import AdapterError and InvalidError from "DS" object HOT 1
- No release with latest commits HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-django-adapter.