Can't populate member variable in javascript

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Can't populate member variable in javascript

Post 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?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Can't populate member variable in javascript

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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...
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

tried to jig it my way baby... ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
        }
Last edited by Chris Corbyn on Wed Sep 06, 2006 12:29 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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"
	}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry buddy, read my edit :)

n00b's method is Object literal.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Man... what is the deal with this?? I tried it n00b's way with no success either. This is very frustrating
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That smells like a bump. ;)
Post Reply