Question

I have a basic blog app, and I can't get the RESTful Adapter working properly with Ember-Data. When I go to the /posts route, it is sending a GET request to the server and the Node server is sending back the right data, but it doesn't seem to be persisting in Ember.

Here's my app:

// Model
App.Post = DS.Model.extend({
    title:          DS.attr(),
    post_content:       DS.attr(),
    tags:           DS.attr(),
    creationDate:   DS.attr() 
});

// Posts route
App.PostsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('post');
    }
});

App.PostsController = Ember.ArrayController.extend({
    sortProperties: ['title'],
    sortAscending: true,

    postsCount: function() {
        return this.get('model.length');
    }.property('@each')
});

// store.js
App.ApplicationAdapter = DS.RESTAdapter;
App.PostAdapter = DS.RESTAdapter;

And the backend:

app.get( '/posts', function( request, response ) {

    post = {post: {
      "id": 1,
      "title": 'A great post',
      "post_content": 'A few brilliant things to say here.',
      "tags": ['JavaScript', 'Underscore'],
      "creationDate": 'Mon, 26 Aug 2013 20:23:43 GMT'
    }};

    console.log(post); // successfully logs the post
    return response.send(post)
});

The data does not show up on the page or in Ember Inspector. I've tried looking for tutorials on Ember-Data for a little help, but they are all fairly outdated and haven't worked for me.

Any insight into the problem would be greatly appreciated! Also, I think that's all of the relevant code, but I can add more if it would help.

Was it helpful?

Solution

Your server should be returning an array of posts:

post = {posts: [{
  "id": 1,
  "title": 'A great post',
  "post_content": 'A few brilliant things to say here.',
  "tags": ['JavaScript', 'Underscore'],
  "creationDate": 'Mon, 26 Aug 2013 20:23:43 GMT'
}]};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top