Some question about variable scope

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Some question about variable scope

yellow1912-2

After reading this tutorial by Mike:
http://www.learningjquery.com/2007/10/a-plugin-development-pattern

I've started to write a plugin which uses ajaxForm to submit multiple
forms on the same page. Each time it submits a form, it should update
a counter to save the current form id, and bind the ajaxForm listener
to the new form that has just been rendered (the previous form is
hidden).

I'm struggling to see how I can save this counter, the only way that
works for me now is to use a global variable, but this will be a
problem if the plugin is used for more than one place on the page
(since they will all use the same global var)

Thank you very much for your help. If my question is unclear at any
point, please let me know

Regards

Raine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Plugins" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/jquery-plugins?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Some question about variable scope

yellow1912-2

After a bit more testing, I tried
// somewhere in my plugin code
$(this).click(function(){
                $.fn.myplugin.currentTabIndex++;
                alert($.fn.myplugin.currentTabIndex);
        }
        );
//

And I bind 2 test elements:
  $("#test1").myplugin();

  $("#test2").myplugin();


They do share the same $.fn.myplugin.currentTabIndex, which is not
what I expected. I wonder if there is anyway to code so that each time
I bind the plugin to a different element then a unique object will be
created which keeps all the variable local.

Any suggestion is greatly appreciated.

Raine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Plugins" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/jquery-plugins?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Some question about variable scope

yellow1912-2

Just an update: it seems like I overcomplicated things, right now i
have:

(function($) {
  //
  // plugin definition
  //

  $.fn.zencart = function(options) {

    var currentTabIndex = 0;
//// some code here

currentTabIndex++;

//// some code here
}
}
)

And it works the way I want

Regards

Raine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Plugins" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/jquery-plugins?hl=en
-~----------~----~----~----~------~----~------~--~---