escalant3 / ember-data-tastypie-adapter

An adapter to connect django applications powered by django-tastypie with ember.js apps
MIT License
102 stars 26 forks source link

No mapping for returned objects #17

Closed ricardogsilva closed 11 years ago

ricardogsilva commented 11 years ago

Hi. I'm going through the ember guides, trying to connect my tastypie API to an ember application. I've pulled the latest ember, ember-data and ember-data-tastypie-adapter code from their respective repos. Getting my data out of the server, I get:

My ember application:

var HortaApp = Ember.Application.create();

HortaApp.store = DS.Store.create({
    revision : 11,
    adapter : DS.DjangoTastypieAdapter.extend(),
});

/* Models */
HortaApp.Garden = DS.Model.extend({
    name : DS.attr('string'),
});

/* Views */
HortaApp.ApplicationView = Ember.View.extend({
    templateName : 'application',
});

HortaApp.AllGardensView = Ember.View.extend({
    templateName : 'all-gardens',
});

/* Controllers */
HortaApp.ApplicationController = Ember.Controller.extend();
HortaApp.AllGardensController = Ember.ArrayController.extend();

/* Routers */
HortaApp.Router = Ember.Router.extend();
HortaApp.Router.map(function(){
    this.resource('allGardens', {path : '/gardens'}, function(){
        this.route('new');
    });
});

HortaApp.AllGardensRoute = Ember.Route.extend({
    model : function(){
        return HortaApp.Garden.find();
    },
    setupController : function(controller, model){
        controller.set('content', model);
    },
});

HortaApp.initialize();

when I visit the http://localhost:8000/#/gardens URL, using Firebug, I can see that the request for data is sent to the server and that the response is correct. Response:

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 2}, "objects": [{"name": "Horta da Velha", "resource_uri": "/api/v1/garden/1/"}, {"name": "Horta do Monte", "resource_uri": "/api/v1/garden/2/"}]}

However, I am getting the following error in the console:

Error: assertion failed: Your server returned a hash with the key objects but you have no mapping for it

What could be the problem? Is my model badly defined? Maybe some convention name I am not respecting?

Thank you

escalant3 commented 11 years ago

One problem might be that the adapter does not expect you to pluralize the name of the model. If you don't want to set up special URL parameters, you should use http://localhost:8000/#/garden. That is the one the adapter is doing automatically when using .find().

Also, the returned data does not include the "id" fields. Are you removing them with a meta option in the server?

ricardogsilva commented 11 years ago

OK, my tastypie URL looks like

http://localhost:8000/api/v1/garden/

It already looked like this before. In the previous post I had shown the ember URL. I think this is what you meant when you said I should not pluralize the model's name.

I was previsouly refering to the URLs that are routed in the ember app. I also changed that URL to:

http://localhost:8000/#/garden/

to be on the safe side.

I also changed the server's response and it now includes the id field (among some others):

{
    "meta": {
        "limit": 20, 
        "next": null, 
        "offset": 0, 
        "previous": null, 
        "total_count": 2
    }, 
    "objects": [
        {
            "active": true, 
            "created": "2013-01-16T00:37:50.779194", 
            "geom": null, 
            "id": 1, 
            "modified": "2013-01-16T00:37:50.779242", 
            "name": "Horta da Velha", 
            "resource_uri": "/api/v1/garden/1/"
        }, 
        {
            "active": true, 
            "created": "2013-01-16T00:38:02.608067", 
            "geom": null, 
            "id": 2, 
            "modified": "2013-01-16T00:38:02.608116", 
            "name": "Horta do Monte", 
            "resource_uri": "/api/v1/garden/2/"
        }
    ]
}

But I am getting the same error as before. Any other hints?

ricardogsilva commented 11 years ago

Ok, I managed to get it to work now.

I guess the latest commit fixed it, as I can now access the objects returned by tastypie. Thanks!

riklaunim commented 11 years ago

If I include tastypie_serializer.js before tastypie_adapter.js it works. The other way around it throws that error :)

escalant3 commented 11 years ago

Exactly. The serializer is a dependency of the adapter. Also, the two files should be used only during development in case you want modify them. You should always build a single file with this methods (https://github.com/escalant3/ember-data-tastypie-adapter#javascript-side) or your own for production.

riklaunim commented 11 years ago

Ruby tools for a Django project? ;) webassets can take care of that on the fly for django projects

escalant3 commented 11 years ago

"Or your own" :)