在我的index.html中我稱之爲Facebook SDK。在那裏,我有以下代碼在我的Backbone應用程序中觸發控制器。Backbone/Facebook API無法按預期工作?
觸發運行該調用獲取登錄的用戶的FB.api()
的功能。
骨幹代碼的第一個例子工作正常,但第二個有一箇中間函數,我將數據傳遞給,但它給了我一個從facebooks all.js
文件縮小的錯誤。
想要這中間功能的原因是可以解僱他們以不同的順序以後。
的Facebook SDK:
FB.Event.subscribe("auth.statusChange", function (response) {
MyApp.trigger("fb:initialize", response);
});
Backbone.js的控制器代碼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的控制器代碼不工作:
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
}
}
我曾嘗試至今使用我認爲所謂的「封閉」。由於FB.api()是異步的,因此this
與window
相關,而不是當前對象。所以我嘗試在通話之外設置一個變量爲this
。但它也行不通。
例如:
var oThis = this;
var apiString = '/' + this.model.id + '/photos';
FB.api(apiString, function(response){
loadPhoto(response, 1, oThis.model);
});
感謝您的回覆,這也不行。我使用下劃線'_.bind()',但它也不起作用。在'all.js'中都給我一個錯誤 – conor909 2015-04-06 10:32:23