this reference in callback function

edited November 2012 in Newbie Questions
Hi ,

Scope of this lost in the callback function. I think this can be achieved with bind function in enyo but I am not aware how to use it exactly in my example.

Please suggest how to implement this in below example.
enyo.kind({
  name: 'GeolocationComponent',
  kind: 'enyo.Control',

create: function() {
    this.inherited(arguments);	
    this.getLocation();
  },
 
published:{
currentLatitude : "",
currentLongitude:""
},

getLocation:function(){
 navigator.geolocation.getCurrentPosition(this.getPositionSuccess, this.getPositionError);
//How to use bind function here so that I can have reference of <b>this </b>in the callback function getPositionSuccess
},

getPositionSuccess:function(position){
// The scope of <b>this </b>is lost here.....
//<b>this </b>belongs to window object here and not to my kind object 

this.setCurrentLatitude(position.coords.latitude);
  this.setCurrentLongitude(position.coords.longitude);
}
});

Comments

  • edited November 2012
    is it the correct way?
    navigator.geolocation.getCurrentPosition(
        enyo.bind(this,this.getPositionSuccess),
        enyo.bind(this,this.getPositionError));
  • That should do it. enyo.bind requires the first argument to be the scope, so this in your example, and the second to be the function, which is fulfilled by this.getPositionSuccess.
  • Please remember to use pre/code tags around code you post. I've fixed the entries above.
Sign In or Register to comment.