I usually do something like this:

function SomeClass()
                       {
                              var thisYouWant = this;
                               map(function(elem){
                                       connect(elem, 'onclick', thisYouWant,
'onItemClick');

                                       },
                                       getElementsByTagAndClassName(
"tr", "ddRow", "newTable"));
.....

The function that you use in the call to map is not called with the same
this object as the function you call map in, so you have to provide it more
explicitly.

-Karl


On 8/24/07, Shiva KM <[EMAIL PROTECTED]> wrote:
>
>
> Hi all,
>         I'm using MochiKit 1.3.1. Oh, and I just tried this out in the
> latest Mochikit from svn too.
>
> I am trying to use map to loop through the rows in a table and using
> "connect" to bind a function to the onclick event to each of the TRs.
> However this throws an error - "funcOrStr' must be a function on
> 'objOrFunc".
>
> After some debugging I found that the reference to the current object
> (this) which I pass in has a different value when it gets inside
> connect ( it refers to the window object, it looks like ). However, if
> I replace the call to map with a for loop, it works fine.
>
> However, I would really like to avoid using the for loop in the
> interest of keeping the code as 'functional' as possible, plus I dont
> really understand why the reference to "this" changes.
>
> I've included the code below to reproduce the problem:
>
> <html>
>         <head>
>                 <script type="text/javascript" src="./scripts/MochiKit/
> MochiKit.js"></script>
>                 <script type='text/javascript'>
>                         function SomeClass()
>                         {
>                                 map(function(elem){
>                                         connect(elem, 'onclick', this,
> 'onItemClick');
>
>                                         },
>                                         getElementsByTagAndClassName("tr",
> "ddRow", "newTable"));
>
>
> /*                              var arr =
> getElementsByTagAndClassName("tr", "ddRow",
> "newTable");
>                                 for (var i = 0; i < arr.length; i++)
>                                 {
>                                         connect(arr[i], 'onclick', this,
> 'onItemClick');
>                                 }
> */
>
>                         }
>
>                         SomeClass.prototype.onItemClick = function()
>                         {
>                                 alert("in item click!");
>                         }
>
>                         function body_OnLoad()
>                         {
>                                 var x = new SomeClass();
>                         }
>                 </script>
>         </head>
>         <body onload="body_OnLoad()">
>                 <table id="newTable">
>                         <tr class="ddRow"><td>hello1</td></tr>
>                         <tr class="ddRow"><td>hello2</td></tr>
>                         <tr class="ddRow"><td>hello3</td></tr>
>                         <tr class="ddRow"><td>hello4</td></tr>
>                         <tr class="ddRow"><td>hello5</td></tr>
>                 </table>
>         </body>
> </html>
>
>
> Cheers,
> shiva
>
>
> >
>


-- 
Karl Norby
[EMAIL PROTECTED]
[EMAIL PROTECTED]
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MochiKit" group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to