Backbone/Facebook API未按预期工作
Backbone / Facebook API not working as expected?
在我的index.html中,我调用了Facebook SDK。在那里,我有下面的代码触发我的主干应用程序中的控制器。
触发器运行一个函数,该函数调用获取登录用户的FB.api()
。
主干代码的第一个示例运行良好,但第二个示例有一个中间函数,我将数据传递给它,但它从缩小的facebooks all.js
文件中返回了一个错误。
想要中间功能的原因是以后能够以不同的顺序发射它们
Facebook SDK:
FB.Event.subscribe("auth.statusChange", function (response) {
MyApp.trigger("fb:initialize", response);
});
backbone.js控制器代码工作:
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);
});
尝试
initialize: function() {
var _this = this;
App.on("fb:initialize", function(event) {
_this.getUser(event.status);
});
},
getUser: function(status) {
if (status === "connected") {
FB.api("/me", function (response) {
if (response && !response.error) {
console.log(response);
}
});
} else {
//do nothing
}
}
或者可替换地使用下划线的CCD_ 6来始终向右绑定this。http://underscorejs.org/#bind
尝试使用listenTo而不是App.on
this.listenTo(App, 'eventName', (function(_this) {
return function() {
return _this.getUser();
};
})(this));
相关文章:
- Facebook登录按钮没有'不能在Firefox上工作
- Backbone/Facebook API未按预期工作
- Facebook scrollTo在iframe中不工作
- Facebook工作历史fql
- 让typescript与facebook登录为Angular 2项目工作
- 无法让 token.remove() 在 Facebook Emitter 中工作
- 试图让Facebook发射器工作
- Omniauth Facebook登录无法工作
- 如何获得Facebook Like Box"黑暗方案”;正确工作?(背景是透明的.)
- Facebook移动Safari无法正常工作
- Facebook自动登录JS在控制台中工作,但在GM脚本中不起作用
- Facebook自动收报机如何工作,例如在某些res或缩放它隐藏
- Facebook滚动条如何工作
- Facebook喜欢在Firefox中不可点击的按钮iframe实现,可以在其他浏览器中工作
- 如何使Facebook喜欢检测在我的网站上工作
- 类似Facebook的按钮是't工作
- jQuerys的draggable不工作包含在facebook页面上(基本理解)
- Facebook javascript sdk在没有ssl的情况下工作
- Facebook登录在浏览器中运行良好,但没有;我不能用phonegap在安卓系统上工作
- Facebook是如何做到的;的URL匹配算法工作