In my index.html I call the Facebook SDK. In there I have the following code that triggers a controller in my Backbone application.
The trigger runs a function that calls the FB.api() that gets the logged in user.
The first example of backbone code works fine, but the second has a middle function that I pass the data to, but it gives me an error back from facebooks all.js file which is minified.
The reasons for wanting the middle function is to be able to fire them in different orders later on.
Facebook SDK:
FB.Event.subscribe("auth.statusChange", function (response) {
MyApp.trigger("fb:initialize", response);
});
backbone.js controller code WORKING:
initialize: function() {
App.on("fb:initialize", this.getUser);
},
getUser: function(event) {
if (event.status === "connected") {
FB.api("/me", function (response) {
if (response && !response.error) {
console.log(response);
}
});
} else {
//do nothing
}
}
backbone.js controller code NOT WORKING:
initialize: function() {
App.on("fb:initialize", this.testFunction);
},
testFunction: function(event) {
var status = event.status;
this.getUser(status);
},
getUser: function(status) {
if (status === "connected") {
FB.api("/me", function (response) {
if (response && !response.error) {
console.log(response);
}
});
} else {
//do nothing
}
}
I have tried so far using what I think is called "closures". As the FB.api() is asynchronous, this relates to the window instead of the current object. So I tried setting a variable to this outside the call. But it also doesn't work.
For example:
var oThis = this;
var apiString = '/' + this.model.id + '/photos';
FB.api(apiString, function(response){
loadPhoto(response, 1, oThis.model);
});
Aucun commentaire:
Enregistrer un commentaire