0

I somehow need to pass a reference to my current object in a callback to phonegap's notification plugin. I'm pretty sure it involves closures but can't quite work it out.

Here's my code:

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']         
        );
      }
    );
  }
}
});

How can I get a reference to 'this' in my delete method?

jbrown
  • 7,518
  • 16
  • 69
  • 117

1 Answers1

0

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']
);

in the line "self.delete.bind(self)", we bind this to the function "this.delete".