Reputation: 931
I purchased play by play of Ember.js at peepcode.com and followed as the video offered.
So, I set the Model and Serializer and Controllers up in Rails.
When I typed URL like this.
http://localhost:3000/actors/wycats
the JSON response rendered as I expected. (It's probably Ember-data's expecting JSON form..right?)
{
"actor": {
"id": 1,
"blog": "http://www.yehudakatz.com",
"company": "Tilde, Inc.",
"email": "[email protected]",
"gravatar_id": "428167a3ec72235ba971162924492609",
"location": "San Francisco",
"login": "wycats",
"name": "Yehuda Katz",
"actor_type": "User"
}
}
So I set up the Store and Actor Model in ember.js
GithubScore.Store = DS.Store.extend({
revision: 11,
adapter: "DS.RESTAdapter"
});
GithubScore.Actor = DS.Model.extend({
login: DS.attr('string'),
name: DS.attr('string'),
gravatarId: DS.attr('string'),
location: DS.attr('string'),
blog: DS.attr('string')
});
And I launched my Ember App, No error occurred.
but when I tried to get a model using console ( I already had a model saved in Rails DB with ID of 1 )
GithubScore.Actor.find(1)
It returns a Class, No error occurred, but When I try to get an attribute of it. it returns only null, although the model's status 'isLoaded'
GithubScore.Actor.find(1).get('isLoaded')
=> true
GithubScore.Actor.find(1).get('blog')
=> null
and I found that when I call GithubScore.Actor.find(1).get('isLoaded') repeatly at first time it returns only false, but when I try to get an attribute 'isLoaded' is changed to true immediately.
GithubScore.Actor.find(1).get('isLoaded')
=> false (for many times)
GithubScore.Actor.find(1).get('blog')
=> null
GithubScore.Actor.find(1).get('isLoaded')
=> true (immediately changed)
and when I try .toJSON() method to model as the video did. It throws an error.
GithubScore.Actor.find(1).toJSON()
=> TypeError: Object <GithubScore.Actor:ember272:1> has no method 'toJSON'
One thing I curious about is that, though GithubScore.Store object is extended from DS.Store.
It doesn't have find(type, id) method which DS.store already has.
I can't find what the problems are. Would you give me some help?
Thank you for reading!
Upvotes: 0
Views: 1925
Reputation: 3745
Try instead to display the blog value in a template and access it through the browser. When you execute GithubScore.Actor.find(1).get('blog')
it's returning null because Ember is retuning merely an object as a Promise while still in the process of fetching it. When you try instead to display the value in the template, the template is bound to the value and will be updated once it's retrieved.
As for isLoaded = true
, apparently it's a bug, i experienced the same issue with RecordArray and it's been reported in previous question on stackoverflow as well from other people.
Upvotes: 3