Javascript关闭:Phonegap和'这个'在回调中

时间:2014-10-26 13:08:55

标签: 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.
      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']         
        );
      }
    );
  }
}
});

我如何能够参考这个'在我的删除方法?

1 个答案:

答案 0 :(得分:0)

尝试:

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”。