I’m building a backbone.js app with facebook api and phonegap.
In my User model, I’m trying to attach a listener to see when the user has logged in.
Myapp.Models.User = Backbone.Model.extend({
initialize: function(){
FB.Event.subscribe('auth.login', function(response){
console.log('logged-in event');
this.get_me();
});
},
get_me: function(){
console.log('use the api to get user details');
}
});
of course, when I do this, the this is (I believe) attached to FB, not Myapp.
I’ve tried
var this_user = this;
FB.Event.subscribe('auth.login', function(response,this_user){
this_user.get_user();
}
but that still fails.
I’ve read all sorts of blogs on how to use this, but can’t seem to get one that I actually understand when working with multiple objects and how do I refer back to the original object.
What you have should work, but note this part:
The
this_userparameter should not be in the anonymous function declaration. That’s because Facebook never passes the second parameter in the callback, so it ends up beingnull; in fact, you should have received a JS error on thethis_user.get_user()statement.