2

I'm trying to use ember data to get json data from my rails backend. Here's the app.js

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: 'http://localhost:3000',
  buildURL: function(record, suffix) {
    console.log(this._super(record, suffix) +'.json');
    return this._super(record, suffix) + '.json';
  }
});
App.Router.map(function() {
  this.resource('posts', { path: '/' }, function() {
    this.resource('post', { path: ':post_id' });
  });
});
App.PostRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', params.post_id);
  }
});
App.PostsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('post');
  }
});

App.Post = Ember.Object.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  created_at: DS.attr('date'),
  updated_at: DS.attr('date')
});

and here's the data being returned by /posts.json

{
  "posts":[
    {
      "id": 1,
      "title": "Some Title",
      "body": "Some text",
      "created_at": "2014-05-04T22:25:35.578Z",
      "updated_at": "2014-05-04T22:25:35.578Z"
    },
    {
      "id": 2,
      "title": "Some Title",
      "body": "Some text",
      "created_at": "2014-05-04T22:25:35.578Z",
      "updated_at": "2014-05-04T22:25:35.578Z"
    }
  ]
}

The this.store.find('post') seems to not be working, but I'm not sure why. After looking through docs and a few tutorials I don't see the problem with the code. here's the error I'm getting.

Error while loading route: TypeError: undefined is not a function
    at Ember.Object.extend.applyTransforms (http://localhost:3000/assets/ember-data.min.js:9:15567)
    at Ember.Object.extend.normalize (http://localhost:3000/assets/ember-data.min.js:9:15707)
    at superFunction [as _super] (http://localhost:3000/assets/ember-1.5.1.js:7724:16)
    at d.extend.normalize (http://localhost:3000/assets/ember-data.min.js:9:18220)
    at superWrapper [as normalize] (http://localhost:3000/assets/ember-1.5.1.js:1293:16)
    at null.<anonymous> (http://localhost:3000/assets/ember-data.min.js:9:19505)
    at Array.map (native)
    at d.extend.extractArray (http://localhost:3000/assets/ember-data.min.js:9:19474)
    at superWrapper (http://localhost:3000/assets/ember-1.5.1.js:1293:16)
    at Ember.Object.extend.extractFindAll (http://localhost:3000/assets/ember-data.min.js:9:16821) 

1 Answer 1

4

You're model is extending the wrong object, I don't see any other obvious errors.

Incorrect

App.Post = Ember.Object.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  created_at: DS.attr('date'),
  updated_at: DS.attr('date')
});

Correct

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  created_at: DS.attr('date'),
  updated_at: DS.attr('date')
});
Sign up to request clarification or add additional context in comments.

2 Comments

Can't believe I missed that. Works great now. Thanks so much!
All good, I had an awesome foo moment like that, extending Router instead of Route, stackoverflow.com/questions/17607675/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.