1

So I started with my code, the paired it down, then paired it down some more, until I was all the way down to the example found on the models page with my code still not working. First here's what's left of my simple code

App.Router.map(function() {
    this.resource("multipost", function() {

    });
});

App.MultipostRoute = Ember.Route.extend({
    model: function() {
        return App.Post.find();
    }
});

App.Store = DS.Store.extend({

});

App.Post = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),

    isPersonOfTheYear: DS.attr('boolean')
});

And my api returns this at the route /posts

{
    "posts": {
        "first_name": "Barack",
        "last_name": "Obama",
        "is_person_of_the_year": true
    }
}

Aside from some basic html I've tried

{{firstName}}
{{posts.firstName}}
{{post.firstName}}

etc in my template and none of them return anything.

I've got a simple controller that spits out the model when I press a button that hasn't spit out anything successfully

App.MultipostController = Ember.ObjectController.extend({
    submitMultiPost: function() {
        console.log(this.get('model').get('firstName'));

        //this.get('model').save();

    }
})

And I've dumped a number of commands to the dev console that haven't returned anything. What could I be missing here? It's trying to do something as it was spitting out errors when I had the json returning "post" instead of "posts" as the root. But no matter what I do or how I poke around I can't find anything in the model. And yes, I've checked the XHR calls and it's successfully making the call and retrieving the data.

The only other odd thing is I have two text fields that are bound back into the model and they'll show up once I type in one of the two fields, until then though the whole model has nothing in it and appears empty. What am I missing here?

jdivock
  • 113
  • 1
  • 1
  • 10
  • Sometimes it can be tricky trying to debug both an ember-app and an ember-data-api-integration at the same time. I think @Darshan's answer is probably right but issues like this will keep happening. Suggest using fixture adapter while coding app, then swap it for rest adapter and hook up to your real api. – Mike Grassotti Jul 05 '13 at 07:21
  • Welp, looks like loading of singular data isn't something that's supported out of the box at the moment which is kind of odd. Look [at](https://github.com/emberjs/data/issues/551) [all](https://github.com/emberjs/data/issues/393) [these](https://github.com/emberjs/data/issues/38) [requests](https://github.com/emberjs/data/issues/97) [and queries](http://stackoverflow.com/questions/12539836/how-to-handle-singular-resources-with-restadapter) – jdivock Jul 05 '13 at 14:38
  • I missed the fact you were trying for singleton resources completely! I was interpreting it as `/post/1`, ie:- a single record. Glad you figured it out. – Darshan Sawardekar Jul 05 '13 at 15:52

2 Answers2

1

Right, so following the advice of a couple other threads, I'm just setting a fake id called 'singleton' and the model is now populating

App.MultipostRoute = Ember.Route.extend({
    model: function() {
        return App.Post.find('singleton');
    }
});

Changed GET to /posts/:id

and the json reponse now looks like

{
    "post": {
        "id": "singleton",
        "first_name": "Barack",
        "last_name": "Obama",
        "is_person_of_the_year": true
    }
}
Community
  • 1
  • 1
jdivock
  • 113
  • 1
  • 1
  • 10
0

If you intend to get a list of posts, then the response body must be an array of posts. Also for both single or multiple posts you must pass an id attribute as well.

{
    "posts": [{
        "id": 1
        "first_name": "Barack",
        "last_name": "Obama",
        "is_person_of_the_year": true
    }]
}

Further for list of posts the controller needs to extend ArrayController. And to get to the post fields you would use {{#each post in controller}} and then use {{post.first_name}} within the loop in your template.

Darshan Sawardekar
  • 5,065
  • 2
  • 21
  • 31
  • Right, this case is a singular record though so shouldn't the given syntax work? – jdivock Jul 05 '13 at 12:03
  • I think I see where this is going now, so what's the convention for loading a single model? In this instance what I'm trying to do is send down a user profile which doesn't have an id and will always have one record. Do I have to write some additional code to make the rest adapter play nice? – jdivock Jul 05 '13 at 12:14
  • Ok, for a singular record the json api should return the object in a `"post": { ... }` wrapper. Try changing the api to use `post` instead of `posts`. – Darshan Sawardekar Jul 05 '13 at 12:14
  • Right, that's what I had originally but that returns: `Assertion failed: Your server returned a hash with the key post but you have no mapping for it` – jdivock Jul 05 '13 at 12:20
  • That doesn't sound right. If you can put up an example online, I can take a look. This stuff really does work. :) – Darshan Sawardekar Jul 05 '13 at 12:42