blaineh
blaineh

Reputation: 2323

Ember route getting empty model after successful promise

I've built a RESTAdapter to work with couchdb, and am testing it to make sure it works, and so far things seem fine, but my test route seems to be having other problems.

Sorry this is so long, I probably ought to set up a fiddle for it... I've just never done that before, but will look into it right now....

I've built the following (relevant) things:

App.Thing = DS.Model.extend({
    rev: DS.attr(),
    price: DS.attr()
});

App.Things<Index>Route = Ember.Route.extend({
    model: function () {
        return this.get('store').findAll('thing');
    }
});

(I've tried ThingsRoute with and without the Index without any change)

In App.Router.map:

this.resource('things', function() {
    this.route('thing', { path: ':thing_id'})
});

In App.ApplicationAdapter = DS.RESTAdapter.extend:

buildURL: function(type, id) {
    id = id || '_all_docs?include_docs=true';
    return this._super(type, id);
}

In App.ApplicationSerializer = DS.RESTSerializer.extend:

extractArray: function(store, type, payload, id, requestType) {
    root = type.typeKey;
    root = Ember.String.pluralize(root);
    newJSON = {};

    newJSON[root] = payload.rows.map(function(row) {
        return row.doc;
    });
    payload = newJSON;

    console.log(payload);
    return this._super(store, type, payload, id, requestType);
},

normalize: function(type, hash, property) {
    var json = { id: hash._id, rev: hash._rev};
    delete hash._id;
    delete hash._rev;

    for (var prop in hash) {
        json[prop] = hash[prop]; 
    }

    console.log(json);
    return this._super(type, json, property);
}

And this template:

<script type="text/x-handlebars" data-template-name="things/index">
    {{#each thing in things}}
        {{thing.rev}}
        {{thing.price}}
    {{else}}
        Empty.
    {{/each}}
</script>

The console.logs in extractArray and normalize both show the following perfectly formatted and correct json:

Object {things: Array[3]}
Object {id: "8117701d38cf9a1112ce8ed38000064d", rev: "1-14918623fedb103cf035ff2489e0a6a1", price: 1}
Object {id: "8117701d38cf9a1112ce8ed3800006e5", rev: "1-09b1e6aa1fb391e11c90bca86daccb7a", price: 5}
Object {id: "8117701d38cf9a1112ce8ed38000144e", rev: "1-2a682bf7ce58829ad2054bb8f5fbe869", price: 4}

but when the template is rendered it simply shows Empty, and when I replace the model hook in the ThingsRoute to this:

return {things: [{id: 1, rev: 234, price: 4}, {id: 2, rev: 235, price: 3}]};

it works exactly as expected. AND when I define afterModel:

afterModel: function(things, transition) {
    console.log(things);
    console.log(transition);
}

It logs this:

Class {type: function, store: Class, isLoaded: true, isUpdating: false, toString: function…}
Transition {router: Router, promise: Promise, data: Object, resolvedModels: Object, providedModels: Object…}

that Class object has this:

content: Array[3]
  0: Class
  1: Class
  2: Class

and each of THOSE Classes has an id field corresponding to my objects.

What's happening? Why isn't my route getting that model even after the Adapter seems to do it's job perfectly?

Upvotes: 2

Views: 2346

Answers (2)

Marcio Junior
Marcio Junior

Reputation: 19128

I think that your problem is because the things variable in your template, doesn't exist, try to update to model

<script type="text/x-handlebars" data-template-name="things/index">
    {{#each thing in model}}
        {{thing.rev}}
        {{thing.price}}
    {{else}}
        Empty.
    {{/each}}
</script>

Or if you want that variable you can create a alias in your controller:

App.ThingsIndexController = Ember.ArrayController.extend({
    things: Ember.computed.alias('model')
});

Upvotes: 1

Kingpin2k
Kingpin2k

Reputation: 47367

You should be using find instead of findAll

Upvotes: 0

Related Questions