I am testing some code in my web console (using coffescript)
@user = new Onethingaday.Models.User()
@user.url= "/users/#{current_user.get('nickname')}.json?id_type=nickname"
@user.fetch()
console.log(@user)
console.log(@user.get("facebook_id"))
The console.log(@user)
line shows the following:
However, console.log(@user.get("facebook_id"))
shows undefined
.
I see that user
has the facebook_id
attribute. How do I retrieve the value of it?
This looks like a timing issue, albeit an odd one. fetch
is asynchronous; you need to wait until its success
callback is called before trying to access any attributes.
Try this:
@user.fetch success: =>
console.log(@user)
console.log(@user.get("facebook_id"))
It's confusing that the first console.log
would show user.attributes.facebook_id
existing and the second wouldn't, but console.log
is itself asynchronous in Webkit's implementation, so what's going on is that the @user.get
call is being resolved immediately (synchronously), whereas the object inspection in the first console.log
is resolving later—after the fetch
completed.
It looks like facebook_id is a property of the attributes property of the @user object. If that's the case, I'd think the following would work:
@user.attributes.facebook_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.