Javascript闭包:Phonegap和回调中的“this”
我需要在phonegap的通知插件的回调中传递对当前对象的引用。我很确定它涉及闭包,但不能完全解决它 这是我的密码:Javascript闭包:Phonegap和回调中的“this”,javascript,cordova,Javascript,Cordova,我需要在phonegap的通知插件的回调中传递对当前对象的引用。我很确定它涉及闭包,但不能完全解决它 这是我的密码: export default Ember.ArrayController.extend({ ... delete: function(buttonIndex) { if (buttonIndex === 1) { // how to access 'this' here? 'self' doesn't work here either. co
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']
);
}
);
}
}
});
如何在delete方法中获取对“this”的引用 试试看:
navigator.notification.confirm
'确实要删除吗?',
self.delete.bindself,//我需要某种类型的闭包将self绑定到这里吗?
“确认删除”,
[‘是’、‘否’]
;
在self.delete.bindself行中,我们将其绑定到函数this.delete。@bergi链接答案中到底与什么相关?所有内容,特别是Felix答案的最后一段。明白了,谢谢。我太急了:-不幸的是,这不起作用。我将对此进行更深入的研究,看看我是否能使它工作。好的,如果我在“delete”方法中使用“this”,这将起作用。谢谢