Sencha Touch - 无法从其他功能中获取值

时间:2014-05-03 17:13:32

标签: javascript extjs geolocation sencha-touch-2

我有2个函数,想从另一个函数调用一个函数并得到2个值。

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;
        }
    });
},

请告知如何正确地做到这一点。

2 个答案:

答案 0 :(得分:2)

当你拨打me时,

Ext.device.Geolocation.getCurrentPosition不再存在。

到那时你处于不同的上下文或范围,Ext.device.Geolocation类对me变量一无所知。

在方法的开头,这指的是你的控制器,但是你正在调用getCurrentPosition类的Ext.device.Geolocation方法,因为你没有将任何参数传递给方法meundefined

它认为你可以用Ext.bind解决这个问题

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

同时寻找电话并在JavaScript中申请:)

最好的问候@ code4jhon

答案 1 :(得分:0)

您可以从getCurrentLocation返回对象,例如:

return {lat:xxxx, lng:yyyy};