Javascript闭包:Phonegap和'这'在回调中
Javascript closure: Phonegap and 'this' in callback
我需要在phonegap的通知插件的回调中传递对当前对象的引用。我很确定这涉及到闭包,但不能完全解决。
这是我的代码:
export default Ember.ArrayController.extend({
...
delete: function(buttonIndex) {
if (buttonIndex === 1) {
// how to access 'this' here? 'self' doesn't work here either.
console.log('Deleting survey with ID=' + this.get('obj_to_delete'));
}
},
actions: {
deleteAction: function(obj_id) {
var self = this;
this.set('obj_to_delete', obj_id);
this.store.find('survey', obj_id).then(function(survey) {
navigator.notification.confirm(
'Are you sure you want to delete?',
self.delete, // do i need some sort of closure binding self to this here?
'Confirm delete',
['Yes', 'No']
);
}
);
}
}
});
如何在我的删除方法中获得对"this"的引用?
try:
navigator.notification.confirm(
'Are you sure you want to delete?',
self.delete.bind(self), // do i need some sort of closure binding self to this here?
'Confirm delete',
['Yes', 'No']
);
在"self.delete.bind(self)"行中,我们将其绑定到函数"this.delete"。
相关文章:
- javascript,将参数传递给函数内部的闭包中的回调
- JavaScript闭包&回调函数
- 使用闭包/函数绑定将自函数作为回调传递
- 使用JavaScript回调函数了解变量作用域和闭包
- 带有 Javascript 回调的闭包
- 非常基本的JavaScript回调/闭包结构
- 闭包的意外行为:回调保存最后一个值
- 尝试使用带有参数的闭包作为回调
- 对闭包变量的更改是否在使用闭包的回调中可见
- 创建带有回调的动态 JSON 数组(内部闭包问题)
- JSONP 回调和闭包/范围
- Is回调函数始终是一个闭包
- Javascript闭包:Phonegap和'这'在回调中
- 参数对应关系:回调与闭包(?)
- 回调中的闭包
- 我需要在DOM事件回调中使用闭包吗
- 使用Google闭包编译器记录回调参数
- 访问子元素内的父作用域's onclick回调(Atom包)
- 维护对“this”的引用在Javascript中使用回调和闭包时
- javascript回调闭包未定义变量