[jQuery] Re: Instantiating multiple plugin instances on the same page
I went back to scratch and did up a simplified rewrite of my plugin. I haven't found the problem with the actual plugin yet, but at least now I know how the architecture is *supposed* to work. Chaining doesn't come into it (as suggested by Sean), but the test code at http://fatdog.com/multiPluginTest/testMultiPlugins.html does demonstrate, as stated by Michael, that each invocation of a function/plugin instantiates a new copy of the function/plugin object, complete with a fresh slate of local variables. Amazing that I didn't know that! :-) It was good to go back to zero, so that I now understand this important concept. Now on to figuring out why my *real* plugin (not that much more complicated than this simple test prototype) isn't working ... Howard On Nov 24, 4:20 pm, howardk [EMAIL PROTECTED] wrote: Michael and Sean, Thanks to both of you. I think I'll take you up on your offer of posting some (simplified) code. Sometime tomorrow if I can find the time for it ... Thanks again, Howard On Nov 24, 3:49 pm, Michael Geary [EMAIL PROTECTED] wrote: You may be worried about a problem that doesn't exist. Every time you call a function, JavaScript creates a new, unique set of local variables for that invocation of the function. It doesn't reuse the same function invocation and its local variables over and over again. Now, you *could* write code that would get you in trouble here. For example, you could use global variables instead of local variables in your function, and those would get overwritten as you might expect. But normal JavaScript behavior does exactly what you want here. This is true for plugin methods just like any other functions. As Shawn suggested, if you have trouble with a specific bit of code, post a link to it and someone can take a look at it. -Mike From: howardk Is there a way of instantiating multiple instances of a plugin on the same page? What I have essentially is an animated effects plugin, and I want to be able to invoke separate instantiations of it, doing something like the following: script $( '#effect_1' ).animEffect( { name: 'jumper', color: 'ff', fps: 30 } ); $( '#effect_2' ).animEffect( { name: 'round-the-moon', color: '00', fps: 40 } ); /script div id='effect_1'/div div id='effect_2'/div I'm still fairly new to javascript. As far as I can tell though, it looks like it can't be done, since as far as I can see, each plugin invocation is really calling the same function object over and over again (thereby overwriting whatever instance or local variables might have been set in a prior invocation). Is this correct? Is there a way of doing this, or am I out of luck? Howard
[jQuery] Re: Instantiating multiple plugin instances on the same page
Depends how your plugin is set up. If you didn't implement the chaining techniques, then chances are you only have one instance. But if you did implement chaining, you *should* have multiple instances. Do you have something like this line in your plugin? return $(this).each( function () { // plugin code goes here }); If you can post your code, we can help more. OR, you can go to the #jquery channel on IRC and use a pastie (www.pastie.org) to get some more interactive help... Hope that helps some. Shawn howardk wrote: Is there a way of instantiating multiple instances of a plugin on the same page? What I have essentially is an animated effects plugin, and I want to be able to invoke separate instantiations of it, doing something like the following: script $( '#effect_1' ).animEffect( { name: 'jumper', color: 'ff', fps: 30 } ); $( '#effect_2' ).animEffect( { name: 'round-the-moon', color: '00', fps: 40 } ); /script div id='effect_1'/div div id='effect_2'/div I'm still fairly new to javascript. As far as I can tell though, it looks like it can't be done, since as far as I can see, each plugin invocation is really calling the same function object over and over again (thereby overwriting whatever instance or local variables might have been set in a prior invocation). Is this correct? Is there a way of doing this, or am I out of luck? Howard
[jQuery] Re: Instantiating multiple plugin instances on the same page
You may be worried about a problem that doesn't exist. Every time you call a function, JavaScript creates a new, unique set of local variables for that invocation of the function. It doesn't reuse the same function invocation and its local variables over and over again. Now, you *could* write code that would get you in trouble here. For example, you could use global variables instead of local variables in your function, and those would get overwritten as you might expect. But normal JavaScript behavior does exactly what you want here. This is true for plugin methods just like any other functions. As Shawn suggested, if you have trouble with a specific bit of code, post a link to it and someone can take a look at it. -Mike From: howardk Is there a way of instantiating multiple instances of a plugin on the same page? What I have essentially is an animated effects plugin, and I want to be able to invoke separate instantiations of it, doing something like the following: script $( '#effect_1' ).animEffect( { name: 'jumper', color: 'ff', fps: 30 } ); $( '#effect_2' ).animEffect( { name: 'round-the-moon', color: '00', fps: 40 } ); /script div id='effect_1'/div div id='effect_2'/div I'm still fairly new to javascript. As far as I can tell though, it looks like it can't be done, since as far as I can see, each plugin invocation is really calling the same function object over and over again (thereby overwriting whatever instance or local variables might have been set in a prior invocation). Is this correct? Is there a way of doing this, or am I out of luck? Howard
[jQuery] Re: Instantiating multiple plugin instances on the same page
Michael and Sean, Thanks to both of you. I think I'll take you up on your offer of posting some (simplified) code. Sometime tomorrow if I can find the time for it ... Thanks again, Howard On Nov 24, 3:49 pm, Michael Geary [EMAIL PROTECTED] wrote: You may be worried about a problem that doesn't exist. Every time you call a function, JavaScript creates a new, unique set of local variables for that invocation of the function. It doesn't reuse the same function invocation and its local variables over and over again. Now, you *could* write code that would get you in trouble here. For example, you could use global variables instead of local variables in your function, and those would get overwritten as you might expect. But normal JavaScript behavior does exactly what you want here. This is true for plugin methods just like any other functions. As Shawn suggested, if you have trouble with a specific bit of code, post a link to it and someone can take a look at it. -Mike From: howardk Is there a way of instantiating multiple instances of a plugin on the same page? What I have essentially is an animated effects plugin, and I want to be able to invoke separate instantiations of it, doing something like the following: script $( '#effect_1' ).animEffect( { name: 'jumper', color: 'ff', fps: 30 } ); $( '#effect_2' ).animEffect( { name: 'round-the-moon', color: '00', fps: 40 } ); /script div id='effect_1'/div div id='effect_2'/div I'm still fairly new to javascript. As far as I can tell though, it looks like it can't be done, since as far as I can see, each plugin invocation is really calling the same function object over and over again (thereby overwriting whatever instance or local variables might have been set in a prior invocation). Is this correct? Is there a way of doing this, or am I out of luck? Howard