Page 1 of 2

Can't populate member variable in javascript

Posted: Tue Sep 05, 2006 4:39 pm
by Luke
I'm having a hard time with my first javascript class... when I attempt to populate this.locations with the geocode() method, it doesn't work. I know that it is finding the locations fine because I tested the return value of getLocations() inside the geocode() method and they were all there. But then when I try to access them by this.locations they aren't there :(

Code: Select all

	function geoLocation(form, map) {
		this.form 		= form;
		this.map 		= map;
		this.address 	= form.address1.value + " " + form.city.value + ", " + form.state.value + " " + form.zip.value;
		this.geocoder 	= new GClientGeocoder;
		this.locations  = null;

		this.find = find;
		this.geocode = geocode;
		
		this.find();
	}
	
	function find() {
		this.geocoder.getLocations(this.address, this.geocode);
		alert(this.locations); // alerts a null value
	}
	
        // For some reason this method doesn't seem to be populating this.locations
	function geocode(response) {
		this.statusCode = response.Status.code;
		if(this.statusCode == 200){
			this.locations = response.Placemark;
                        // alert(this.locations); // alerts me with some locations it found
		}
	}
EDIT: I fiddled with it a little more and came to realize that the geocode function is behaving as if it is a function on its own rather than a method of the getLocation class... why would it be doing that? Could it have something to do with the fact that I am using it as a callback function?

Posted: Wed Sep 06, 2006 11:40 am
by n00b Saibot
use following way for declaring a new object/class...

Code: Select all

var geoLocation = function (aForm, aMap) {

  form      : aForm, 
  map       : aMap, 
  address   : form.address1.value + " " + form.city.value + ", " + form.state.value + " " + form.zip.value,
  geocoder  : new GClientGeocoder,
  locations : null,

  find      : function () {
                this.geocoder.getLocations(this.address, this.geocode);
                alert(this.locations);
              } 

  geocode   : function geocode(response) {
                this.statusCode = response.Status.code;
                if(this.statusCode == 200) { 
                   this.locations = response.Placemark;
                   alert(this.locations);
              }
        }

  }
try it and lemme know how it goes...

edit | I see you have hardcoded a form name as form, maybe its cause of bug... I would say make it be passed by reference too.

Re: Can't populate member variable in javascript

Posted: Wed Sep 06, 2006 11:58 am
by Chris Corbyn
The Ninja Space Goat wrote: EDIT: I fiddled with it a little more and came to realize that the geocode function is behaving as if it is a function on its own rather than a method of the getLocation class... why would it be doing that? Could it have something to do with the fact that I am using it as a callback function?
Because you should do it like this:

Code: Select all


function yourClass()
{
    this.foo = 42;

    this.yourMethod = function()
    {
        return this.foo;
    }
}

It'll work the other way though because that's all public.

Code: Select all


function yourClass()
{
    var foo = 42; //Private

    this.yourMethod = function()
    {
        return foo;
    }
}

By leaving out "this" you make the property or method private.

Posted: Wed Sep 06, 2006 12:17 pm
by n00b Saibot
d11wtq wrote:By leaving out "this" you make the property or method private.
but he -did- use this...

Code: Select all

this.find = find; 
this.geocode = geocode;
didn't he?

Posted: Wed Sep 06, 2006 12:21 pm
by Luke

Code: Select all

	function MyGeocoder(address){
		this.locations;
		this.geocoder = new GClientGeocoder;
		// Populate locations with my finder method
		this.geocoder.getLocations(address, this.find); // This is telling me that this.find isn't a function
		
		this.find = function(response){
			alert(response);
		}
	}
This doesn't work... it's telling me that this.find isn't a function...

Posted: Wed Sep 06, 2006 12:25 pm
by n00b Saibot
tried to jig it my way baby... ?

Posted: Wed Sep 06, 2006 12:27 pm
by Chris Corbyn
The Ninja Space Goat wrote:

Code: Select all

	function MyGeocoder(address){
		this.locations;
		this.geocoder = new GClientGeocoder;
		// Populate locations with my finder method
		this.geocoder.getLocations(address, this.find); // This is telling me that this.find isn't a function
		
		this.find = function(response){
			alert(response);
		}
	}
This doesn't work... it's telling me that this.find isn't a function...
Oh wait is that constructor logic? Stick it down at the bottom so that it runs after the methods inside it have been defined.

EDIT | Like this:

Code: Select all

        function MyGeocoder(address){
                this.locations;
                this.geocoder = new GClientGeocoder;
               
                this.find = function(response){
                        alert(response);
                }
            
                // Populate locations with my finder method
                this.geocoder.getLocations(address, this.find);
        }

Posted: Wed Sep 06, 2006 12:28 pm
by Luke
what is your way? I've never seen that before... I will be more than happy to try it... but I would like to understand it before just plopping it into my code... can you explain?

This doesn't work either...

Code: Select all

	function MyGeocoder(address){
		this.locations;
		this.geocoder = new GClientGeocoder;
		this.find = find;
		// Populate locations with my finder method
		this.geocoder.getLocations(address, this.find);
		
		function find(response){
			this.locations = response;
			alert(this.locations); // alerts "[object Object]"
		}
	}
	
	function loadAddress(){
		var address = '5047 Arden Way, Paradise, CA 95969';
		var Geocode = new MyGeocoder(address);
		alert(Geocode.locations); // alerts "undefined"
	}

Posted: Wed Sep 06, 2006 12:30 pm
by Chris Corbyn
Sorry buddy, read my edit :)

n00b's method is Object literal.

Posted: Wed Sep 06, 2006 12:33 pm
by Luke
Still not working...

Code: Select all

	function MyGeocoder(address){
		this.locations;
		this.geocoder = new GClientGeocoder;
		
		this.find = function(response){
			this.locations = response;
			alert(this.locations); // alerts "[object Object]"
		}
		
		// Populate locations with my finder method
		this.geocoder.getLocations(address, this.find);
	}
	
	function loadAddress(){
		var address = '5047 Arden Way, Paradise, CA 95969';
		var Geocode = new MyGeocoder(address);
		alert(Geocode.locations); // alerts "undefined"
	}
Object literal... I will have to put that on the research list...

I think that the fact that it is being used as a callback function is messing up its state or something... it's like it has readonly access to all of the objects properties. It can read them, but changing them only effects them with in that particular function... nowhere else... :( I am getting pretty frustrated

Posted: Wed Sep 06, 2006 12:55 pm
by Chris Corbyn
What does GClientGeocoder look like?

Callbacks between objects usually work from what I remember. Let's try it :) Might post back but someone's entising me to go downstairs and eat my dinner (thai green curry) :D

Posted: Wed Sep 06, 2006 1:01 pm
by n00b Saibot
d11wtq wrote:What does GClientGeocoder look like?
yeah... suppose you gave us the code so we could look into/experiment with it.

Posted: Wed Sep 06, 2006 1:04 pm
by Luke

Posted: Wed Sep 06, 2006 4:22 pm
by Luke
Man... what is the deal with this?? I tried it n00b's way with no success either. This is very frustrating

Posted: Wed Sep 06, 2006 4:51 pm
by feyd
That smells like a bump. ;)