0

Details:

I am getting data for the html5 Geolocation; specifically longitude and latitude. I am trying to create a plugin that anyone can call from within a project and get the long and lat.

I have read this How to return response from an ajax call but I wondering how I should call these functions.

I placed a extjs tag even though I think it's more of a javascript core question

JavaScript:

Ext.define('Ext.ux.GeoLocation', {
    alias: 'plugin.ux.geolocation',

    constructor: function() {
        console.log('called 1');
        this.duh = 'yes';
        this.init();

    },

    init: function () {
        console.log('called 2');
        navigator.geolocation.getCurrentPosition(this.test, this.test);        
    },

    test: function(init) {
        console.log('called 3');
        this.duh = "amazing";
    }

});

var geo = new Ext.ux.GeoLocation();
geo.test(); // this gets called twice though... incorrect? 

Here is a FIDDLE

The console out output is:

called 1 
called 2 
called 3 
called 3 

Is this a bad practice? Is there another solution?

1 Answer 1

2

On your class constructor you're passing the test function to navigator.geolocation.getCurrentPosition. Do you know what that other function do? most likely is calling test internally.

You can check by doing:

var geo = new Ext.ux.GeoLocation();
console.log("middle");
geo.test();

Then you should see "middle" printed between the two "called 3" lines.

Sign up to request clarification or add additional context in comments.

4 Comments

So, am I fine with just calling geo.test()? At this point by the time I get to geo.test() the getCurrentPosition() has already fired... correct?
I don't know extjs but it seems that "init" is some kind of "constructor" that gets invoked on object construction. So yes, getCurrentPosition() is getting executed on object construction (that's why you also see the "called 2" printed before "called 3")
I'm calling this.init() in the constructor. The constructor gets called internally. I actually removed init() and it still works...
nope, I was asking the wrong question. what i was looking for was impossible for javascript, your answer let me to it though, so thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.