toranb / ember-data-django-rest-adapter

An ember-data adapter for django web applications powered by the django-rest-framework
MIT License
152 stars 26 forks source link

Configuring the JSON root for DjangoRESTAdapter #104

Closed nikolas closed 10 years ago

nikolas commented 10 years ago

Hi, I have an API response from Django REST Framework v2.4.3 that looks like this, for /api/puzzles/

{
    "count": 1, 
    "next": null, 
    "previous": null, 
    "results": [
        {
            "display_name": "test puzzle", 
            "description": "test puzzle description", 
            "created_at": "2014-10-01T17:57:25.223Z", 
            "updated_at": "2014-10-01T17:57:25.223Z"
        }
    ]
}

I've connected this to my Ember application with DS.DjangoRESTAdapter. When I run WeatherRoulette.__container__.lookup('store:main').find('puzzle'), ember-data says it's expecting a different format:

Error: Assertion Failed: The response from a findAll must be an Array, not {count: 1, next: null, previous: null, results: [object Object]}
    at new Error (native)
    at Error.EmberError (http://localhost:8000/site_media/WeatherRoulette/dist/assets/vendor.js:26705:23)
    at Object.Ember.assert (http://localhost:8000/site_media/WeatherRoulette/dist/assets/vendor.js:16889:15)
    at http://localhost:8000/site_media/WeatherRoulette/dist/assets/vendor.js:73434:15
...

I've forgotten how to customize the JSON root for ember-data. I just want to point it to results. Is there a simple config change for this? I'm using Ember 1.7.0, ember-data 1.0.0-beta.10.

Thanks.

holandes22 commented 10 years ago

See #80, this adapter does not support pagination yet. To make it work you either need to modify the adapter (meaning, fix #80) or disable pagination on DRF

benkonrath commented 10 years ago

I'm working on updating the adapter to properly support ember-data beta.10 (#101). I think I should be able to get pagination working but I'm not sure if I'll hit some problems yet. I'm a bit busy with other work now but I'm hoping to get back to this shortly.

nikolas commented 10 years ago

I'm looking at #101 and trying to fix those test failures. It looks like there's a lot of methods from DS.RESTAdapter to re-implement.

Maybe it would be better to configure django-rest-framework to return the JSON format that DS.RESTAdapter / DS.ActiveModelAdapter expects. I'm looking into that now.

benkonrath commented 10 years ago

Yeah, there's a some code for this by @Goldcap pointed to in #80

https://github.com/Goldcap/operis

I think the adapter needs a full rework. I guess it's just easy to deal with this server side like you suggest but there's still a benefit to having an adapter work with the default django-rest json when you can't change the server bits.

The code I have right now is processing the json into the right format on the JavaScript side instead of the server side.

If you just want to get something going, you can always use the adapter with ember-data beta.8 with ember 1.7.0. I'm using it in a project and it seems to be working ok.

Goldcap commented 10 years ago

Hey, thanks for the shoutout, and yes, I eventually decided to implement most of the changes in Django. Also, note, my paginator only works for parent records, I had to implement a separate solution for pagination of many-to-many or one-to-many records.

Ping me back if anyone wants to see it, this WIP is fairly old already :)

benkonrath commented 10 years ago

@Goldcap If you're interested / have time it would be cool to see that work extracted to make it easier to find. I don't mean to add more work to your plate though. :)

Goldcap commented 10 years ago

Hi Ben;

Absolutely, will upload probably this weekend. Sorry for the very late reply! Everyone loves pagination.

On Fri, Oct 3, 2014 at 9:48 AM, Ben Konrath notifications@github.com wrote:

@Goldcap https://github.com/Goldcap If you're interested / have time it would be cool to see that work extracted to make it easier to find. I don't mean to add more work to your plate though. :)

— Reply to this email directly or view it on GitHub https://github.com/toranb/ember-data-django-rest-adapter/issues/104#issuecomment-57798370 .

"If there is Evolution, why are there monkeys?" - Larry King

dustinfarris commented 10 years ago

Will be addressed in dustinfarris/ember-django-adapter#22