object and functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dreamfalcon
    New Member
    • Oct 2007
    • 20

    object and functions

    Hi
    I had some variables and functions to work with google maps, that are working great, but i needed to have more than one map in the same page, and was using different names to each variable, like xml1, xml2. But decided to use a object to group all variables and functions.

    Code:
    function Map(){
    	this.xml;
    
    	this.loadMap = function (tabId){
    		this.myElement = $(tabId);
    		if (GBrowserIsCompatible()) {
    			this.map = new GMap2(this.myElement.getElementsByClassName("googlemap")[0]);
    		...
    		this.getCells(tabId);
    		}
    	}
    
    	this.getCells = function(){
    		GDownloadUrl("request.php?script=genxml.php", function (data){
    			this.xml = GXml.parse(data);
    			this.redraw();
    		});
    
    	this.redraw = function (){
    		alert("ok");
    		//code to draw cells
    	}
    }
    and
    Code:
    map1= new Map();
    map1.loadMap(tabId);
    it gives this error
    "this.redra w is not a function"

    and map1.xml is empty.

    I don´t have many experience with objects in Javascript
    Can anyone help me?
    thx
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Add a closing brace and a semicolon }; after line 17

    Comment

    • dreamfalcon
      New Member
      • Oct 2007
      • 20

      #3
      Originally posted by hsriat
      Add a closing brace and a semicolon }; after line 17
      sorry, copy paste error :D

      Code:
      function Map(){
      	this.xml;
      
      	this.loadMap = function (tabId){
      			this.myElement = $(tabId);
      		if (GBrowserIsCompatible()) {
      			this.map = new GMap2(this.myElement.getElementsByClassName("googlemap")[0]);
      		...
      		this.getCells(tabId);
      		}
      	}
      
      	this.getCells = function(){
      		GDownloadUrl("request.php?script=genxml.php", function (data){
      			this.xml = GXml.parse(data);
      			this.redraw();
      		});
            }
      	this.redraw = function (){
      		alert("ok");
      		//code to draw cells
      	}
      }
      and
      Code:
      map1= new Map();
      map1.loadMap(tabId);

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Is the error still there?

        Comment

        • dreamfalcon
          New Member
          • Oct 2007
          • 20

          #5
          Originally posted by hsriat
          Is the error still there?
          "this.redra w is not a function"
          Yes, same error :(

          Maybe because is a function inside a function, it doesn't recognize as the same object?

          Comment

          • dreamfalcon
            New Member
            • Oct 2007
            • 20

            #6
            "In JavaScript this always refers to the “owner” of the function..."

            In my case the “owner” becomes GDownloadUrl.
            After a google search i think that this is called "Higher order functions", but no ideas how to solve this.

            Comment

            • dreamfalcon
              New Member
              • Oct 2007
              • 20

              #7
              SOLVED
              -> var me=this;

              Code:
              function Map(){
              	this.xml;
              	var me=this;
              ...
              
              	this.getCells = function(){
              		GDownloadUrl("request.php?script=genxml.php", function (data){
              			me.xml = GXml.parse(data);
              			me.redraw();
              		});
                    }
              
              }

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                That's good you solved it!!

                Even I hate the fact that I don't understand OOP. Will start from a..b..c.. in some free time.

                Comment

                Working...