473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I call a function in a proto type object

Claus Mygind
571 512MB
I keep getting "MyControl.displayMyWayPoint is not a function" error

Expand|Select|Wrap|Line Numbers
  1. var Class = {
  2.   create: function() {
  3.     return function() {
  4.       this.initialize.apply(this, arguments);
  5.     }
  6.   }
  7. }
  8.  
  9. var MyControl = Class.create();
  10. MyControl.prototype = {
  11.  
  12. ...more code above... 
  13.  
  14.    displayMyWayPoint: function() {
  15.       this.mc.map.clearOverlays();
  16.       this.mc.drawMyWaypoint("41.880150027573109","-87.80519999563694");
  17.    },
  18.  
  19. ... more code below .....
  20.  
  21. }
  22.  
  23. td.innerHTML = '<input type="button" id="'+waypointObject[i].waypoint+'btn" value="Map it" onclick="MyControl.displayMyWayPoint('+waypointObject[i].lat+','+waypointObject[i].lon+');"/> '+(s+1)+".";
  24.  
  25.  
basically I create a table on the fly. Each row has a button with an onclick( ) event handler that I want to call the function displayMyWayPoint( ) in the proto typed control.

How do I do that?
Mar 11 '10 #1
8 2253
RamananKalirajan
608 512MB
HI Claus,
Here with I am giving a sample for prototyping. Please look into it. The code you have written have some problem with argument passing.

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.  
  3.  
  4. function cat(name) {
  5.     this.name = name;
  6.     this.talk = function() {
  7.         alert( this.name + " say meeow!" )
  8.     }
  9.  
  10. cat1 = new cat("felix")
  11. cat1.talk() //alerts "felix says meeow!"
  12.  
  13. cat2 = new cat("ginger")
  14. cat2.talk() //alerts "ginger says meeow!"
  15.  
  16. cat.prototype.changeName = function(name) {
  17.     this.name = name;
  18. }
  19.  
  20. firstCat = new cat("pursur")
  21. firstCat.changeName("Bill")
  22. firstCat.talk() //alerts "Bill says meeow!"
  23.  
  24. </script>
Thanks and Regards
Ramanan Kalirajan
Mar 12 '10 #2
Claus Mygind
571 512MB
Your example that you quoted from the JavaScriptKit web site is understandable. However, I am not the original creator of the prototyped control that I show above.

What I want to do is use some of the functions in the custom control. So I guess what I am really asking how can I tap into the already created custom control and have it process my information?

Also what further would you need to see in order to help me here?
Mar 12 '10 #3
RamananKalirajan
608 512MB
In the function call, displayMyWayPoint() you are sending two parameters. But in the prototype.. you have written a function without any parameters. What about that? How you are directly using the prototype "MyControl" directly in you code. You should create a object for that and use it.

Thanks and Regards
Ramanan Kalirajan
Mar 15 '10 #4
Claus Mygind
571 512MB
Yes I was aware I did not receive the parameters. My intent was to add them later. My problems was calling the function in the first place.

I have now got it working not using prototyping which is a shame as I would really like to learn how to integrate it into my programming.

I understand how to create a custom object.
I understand how to add methods to a custom object
I understand how to bind the custom object to an element that already resides on the form.

What I really did not understand, was how to bind a custom class with methods to an element that I add later dynamically to the form.

As you saw in my example, I receive xml data from my GPS unit with waypoints, lat and lon.

I parse the data, then I create a table and fill it with parsed xml data.
I used the .innerHTML to create a button on each row displayed.

This newly created button should be bound to the custom object (or at least the method in the custom object) that will call the mapping routine. This of course is where the parameters are sent "lat" and 'lon". That part will be a snap if I could figure out how to bind the prototyped method to the "onclick" event handler of the button.

Any thoughts on this would be appreciated

Thanks for your input.
Mar 15 '10 #5
RamananKalirajan
608 512MB
Hi Claus,

i have created a code for your requirement. I am not sure, whether you are looking for this one.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. function MyControl(lat, lon) {
  4.  this.lat = lat;
  5.  this.lon = lon;
  6. }
  7.  
  8. MyControl.prototype.displayMyWayPoint = function () {
  9.     alert(this.lat+"  "+this.lon)
  10. };
  11.  
  12.  
  13. var newObj = new MyControl(1.414,1.632);
  14.  
  15.  
  16. var newObj2 = new MyControl(6.61514,8.25414);
  17. newObj2.displayMyWayPoint();
  18. newObj.displayMyWayPoint();
  19. </script>
I am not that much good at scripting.

Thanks and Regards
Ramanan Kalirajan
Mar 15 '10 #6
Claus Mygind
571 512MB
Ramanan, Thanks for your continued quest to help me solve this problem. I get what you are saying but I don't know how to fit it into what I am doing.

Below is the code I used to solve my problem, but it is not through binding a prototype method to the button I am creating.

Below my code is how the onclick event handler is prototyped for elements that are on the page when it is served up.


Expand|Select|Wrap|Line Numbers
  1.  
  2. function timeStampRecs()
  3. {
  4.         var x =  document.getElementById("dataString").value;
  5.  
  6.         //extract all wanted info from x
  7.         var y = x.slice(x.indexOf("<wpt"), x.indexOf("<trk>") )
  8.         var z = x.slice(x.indexOf("<trk>") )
  9.  
  10.         //store each way point in an array split on the double line return between each way point
  11.         waypointArray = y.split("\n\n");
  12.  
  13.         //extract finds into waypoint object
  14.         for (i=0; i < waypointArray.length; i++ )
  15.         {
  16.             //here I get the info I need from the xml file and store it into several arrays
  17.         }
  18.  
  19.         var tr, td;
  20.         var tbody = document.getElementById('caches');
  21.  
  22.         aSort = new Array()
  23.         //itmes are sorted into a parallel array
  24.         for (i=0; i < waypointObject.length; i++ )
  25.         {
  26.             aSort[aSort.length] = waypointObject[i].sort;
  27.         }
  28.         aSort.sort();
  29.  
  30.         /*
  31.         _____________________________________________________
  32.  
  33.         Here I fill in the content of my table.  
  34.         This is where I start to create dynamic elements
  35.         _____________________________________________________
  36.  
  37.         */
  38.         for (s=0; s < aSort.length ; s++ )
  39.         {
  40.             tr = tbody.insertRow(tbody.rows.length);
  41.  
  42.             td = tr.insertCell(tr.cells.length);
  43. /*
  44. _____________________________________
  45.  
  46. This is where I would like to bind 
  47. the prototype method.  My problem
  48. is this, how to add the onclick
  49. event handler
  50. _____________________________________
  51.  
  52. */
  53.             td.innerHTML =     '<input type="button" id="'+waypointObject[i].waypoint+
  54.                             'btn" value="Map it" onclick="mapIt('+waypointObject[i].lat+','+
  55.                             waypointObject[i].lon+');"/> '+(s+1)+".";
  56.         }
  57. }
  58.  
  59.  
  60. function mapIt(lat,lon)
  61. {
  62.     /*
  63.     _____________________________________
  64.  
  65.     the function below is a prototype
  66.     method of the class display, which
  67.     is created when the page is loaded.
  68.     _____________________________________
  69.  
  70.     */
  71.     display.displayMyWayPoint(lat,lon);
  72. }
  73.  

This is the simplified page that is served up. It links to an external .js file scripts.js The forms onload loads and initializes the prototypes

On the page is one button "drawMyWayPointButton"

Below this example are the relevant portions of the scripts.js file which bind the onclick event handler to the button.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  5. </head>
  6.  
  7. <script type="text/javascript" src="Scripts.js">*</script>
  8.  
  9. <body onload="load()">
  10.  
  11. <div id="readBox">
  12.  
  13.     <input type="button" value="draw waypoint" id="drawMyWayPointButton" />
  14.  
  15. </div>
  16.  
  17. <div style="background-color:#3DA831;">
  18.     <table  id="myOutput">
  19.         <tbody id='caches'>
  20.             <tr>
  21.                <td>Cache #</td>
  22.             </tr>
  23.         </tbody>
  24.     </table>
  25. </div>
  26.  
  27. </body>
  28. </html>
  29.  
  30.  
Here is the script file
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3. a variable display is created. then the load function runs after the page has loaded and all the elements are there including the button.
  4. */
  5.  
  6.     var display;
  7.  
  8.     function load() {
  9.         display = new GarminDeviceControlDemo(
  10.             "statusText", 
  11.             "readMap",
  12.             ["http://developer.garmin.com/","ee3934433a35ee348583236c2eeadbc1"]
  13.         );        
  14.     }
  15.  
  16. var Class = {
  17.   create: function() {
  18.     return function() {
  19.       this.initialize.apply(this, arguments);
  20.     }
  21.   }
  22. }
  23.  
  24. var GarminDeviceControlDemo = Class.create();
  25. GarminDeviceControlDemo.prototype = {
  26.  
  27.     initialize: function(statusDiv, mapId, keysArray) {        
  28.         this.status = $(statusDiv);
  29.         this.mc = new Garmin.MapController(mapId);
  30.         this.factory = null;
  31.         this.keys = keysArray;
  32.  
  33. */
  34. ______________________________
  35.  
  36. Here the button which is loaded with 
  37. the page is initialized into the proto
  38. typed object 
  39. GarminDeviceControlDemo.prototype
  40.  
  41. This can be done because the 
  42. button already exists.  In my
  43. case the buttons I want to bind
  44. have not yet been created on 
  45. the form
  46. ______________________________
  47.  
  48. */
  49.  
  50.         this.drawMyWayPointButton = $("drawMyWayPointButton");
  51.  
  52. /*
  53. ______________________________
  54.  
  55. Next you can see that the "onclick"
  56. event handler is added to the button.
  57. ______________________________
  58.  
  59. */
  60.  
  61.         this.drawMyWayPointButton.onclick = function() {
  62.             this.displayMyWayPoint();
  63.         }.bind(this);
  64.     },
  65.  
  66. /*
  67. ______________________________
  68.  
  69. Finally here is the function that the
  70. button calls when clicked.  This
  71. is the same function I call in my
  72. own example above. It is a function
  73. of the variable "display".
  74.  
  75. So again how do I bind this to
  76. buttons in the same way that is
  77. done in this example????????
  78. ______________________________
  79.  
  80. */
  81.     displayMyWayPoint: function(lat,lon) {
  82.         this.mc.map.clearOverlays();
  83.         this.mc.drawMyWaypoint(lat,lon);
  84.     },
  85. };
  86.  
Mar 15 '10 #7
RamananKalirajan
608 512MB
Can you please send the html and js as a attachment in your next reply.. so that i can try on it.

Thanks and Regards
Ramanan Kalirajan
Mar 16 '10 #8
Claus Mygind
571 512MB
Thanks for wanting to take it further. But alas it will do you no good to get the full html and js files as they require a Garmin GPS to get to the problem in question. If you cannot load the external data you can not test the function. Might I suggest, if you wish to proceed with helping that you simply develop a class with a method as described above, then create a routine that adds a button to the form using .innerHTML. You then have the same components that I have. From there try adding the method from the custom class you created to the button. A simple onclick = "hello world" would do the trick.
Mar 18 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rob Long | last post by:
Hi, I've got this really annoying problem, which I think might be a bug, but I wanted to check that I'm not missing some subtle PHP feature. I've created a user-defined list type - a bit like...
3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
7
by: QQ | last post by:
I have a function defined in CC file like int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short g2p, bool s ); I put it in the header file as int D(char* p, char* e, char* c,...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
2
by: Marek | last post by:
Hi I'm trying to call a native C++ dll using the following code (both C# and C++ code segments included). When I make the call to the method (AddTwoDoubles) that has no return value all is fine. ...
1
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is...
9
by: toton | last post by:
Hi, Which one is the correct syntax for constructor for static memory allocation Object obj("param"); or Object obj = Object("param"); 1) For the first one, how compiler checks it as not a...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.