Ⓙⓐⓚⓔ wrote:
> not 100% sure but something like that happened to me!
> 
> mydata, gets re-used! that's why we use the weird case of $.extend()
> to make a fresh object instead of just using it....
> 
>>>           var mydata = { check_dirty: 1 };
> 
> 
> seems to get shared!
> 
> 
>>>>>> var mydata = jQuery.extend({},{ check_dirty: 1 })
> makes a fresh mydata object
> there may be a better way of coding this... but it got me out of a
> similar situation!

Jake,

Thank you for the suggestion. I will give it a try tonight.

<reading, reading, reading, ...>

I think I know why this is happening.

When you create a named function is is basically a static object stored 
in funcname of the scope it was defined in. when you declare a var in a 
function it is also a static object attached to the function object. As 
such mydata is a single static object and effectively a single object in 
the global space of objects. So repeated calls to funcname that set 
mydata will result in the last calls values be stored into mydata.

With the anon function, you are actually creating multiple functions 
each with its own mydata variable defined within each anon function. The 
var statement DOES NOT work like it does in C where the variable is 
created on the stack and is unique to that function call at runtime.

Does this sound right?

So the real way around this problem, I think, is to create an object and 
use the new constructor and attach you local instance variables to 
"this". Which is easier to say than to do for me, because my brain 
doesn't think this way yet.

-Steve

> On 1/26/07, Stephen Woodbridge <[EMAIL PROTECTED]> wrote:
>> Ⓙⓐⓚⓔ wrote:
>>>           $(".button").bind( "click", function(){       });
>>> keeps everything associated with each individual button, throwing in a
>>> named function can easily hit some globals!
>>>
>>> and what is form_dirty?
>> form_dirty is a boolean global that gets set it my pseudo-form get
>> modified, so I can ignore asking the use to save if they move off the page.
>>
>>> let's see the whole page!
>> http://imaptools.com/sm/index-002.html - with named function
>> http://imaptools.com/sm/index-003.html - with anon function
>>
>> To test the page, load it can click the "GO" button, it should call
>> action_go() and load an image and the select list, this work with the
>> anon function.
>>
>> I want to extract the named function so I can reuse it with the onchange
>> event on #files to ask the user to save if the form is dirty. You can
>> make the form dirty by clicking on the image.
>>
>> -Steve
>>
>>> On 1/25/07, Stephen Woodbridge <[EMAIL PROTECTED]> wrote:
>>>> I have a bunch of buttons, and use the following to assign click events.
>>>>
>>>>              $(".button").each( function() {
>>>>                  var data = { check_dirty: 1 };
>>>>                  data.action = $(this).text().toLowerCase();
>>>>                  if (data.action == 'save') data.check_dirty = 0;
>>>>
>>>>                  $(this).css("text-align", "center");
>>>>                  $(this).bind( "click", data, check_save);
>>>>              });
>>>>
>>>>
>>>> The problem is that all the click handlers get the data that is assign
>>>> by the last button processed. If I set through firebug, they are all
>>>> getting assigned the correct values as the each loop is executing, but
>>>> data seems to be global rather then scoped to the anon function!
>>>>
>>>> What am I missing here?
>>>>
>>>> -Steve
>>>>
>>>> _______________________________________________
>>>> jQuery mailing list
>>>> discuss@jquery.com
>>>> http://jquery.com/discuss/
>>>>
>>>
>>
>> _______________________________________________
>> jQuery mailing list
>> discuss@jquery.com
>> http://jquery.com/discuss/
>>
> 
> 


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to