문제

I have 2 functions and would like to call one function from another and get 2 values.

renderMap: function(){
    // here I need to call getCurrentLocation function and get 2 values - lat and lng from it.
    console.log(lat, lng);
},

getCurrentLocation: function() {
    var me = this,
        lat,
        lng;

    Ext.device.Geolocation.getCurrentPosition({     
        success: function(position) {
            me.lat = position.coords.latitude;
            me.lng = position.coords.longitude;
        }
    });
},

Please advise on how to do this properly.

도움이 되었습니까?

해결책 2

You can return object from getCurrentLocation, for example:

return {lat:xxxx, lng:yyyy};

다른 팁

me doesn't exist anymore when you call Ext.device.Geolocation.getCurrentPosition.

You are in a different context or scope by then, Ext.device.Geolocation class knows nothing about me variable.

At the beginning of your method this refered to your controller, but then you are invoking getCurrentPosition method of Ext.device.Geolocation class, since you are not passing any parameters to the method me should be undefined.

It think you can solve this with Ext.bind

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-bind

Also look for call and apply in JavaScript :)

Best regards @code4jhon

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top