[jQuery] Re: passing args to a delegate

2008-12-15 Thread Jan Limpens

javascript just keeps amazing me more and more :)
thanks for the insight!

On Sat, Dec 13, 2008 at 8:49 PM, Jeffrey Kretz jeffkr...@hotmail.com wrote:

 Mike's suggestion involves creating a javascript closure.

 There are a number of good articles on closues.

 http://www.google.com/search?hl=enq=javascript+closuresrlz=1W1GGLL_en

 But essentially, when you create a function that has a reference to a value 
 outside of its scope, that function is created as a closure, with its own 
 context that has access to those variables, even when they've been changed on 
 the global scope.

 Try this:

 var x = 25;
 var fn = function(val){
   return function(){
  alert(val);
   };
 }(x);
 x = 30;
 fn();

 The alert will be for 25, rather than 30, as the closure has its own context 
 now for the x variable.

 The downside to closures is that handled incorrectly, they can cause memory 
 leaks.

 But review of the many articles on the subject will help keep that from 
 happening.

 JK


 -Original Message-
 From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On 
 Behalf Of Jan Limpens
 Sent: Saturday, December 13, 2008 2:28 PM
 To: jquery-en@googlegroups.com
 Subject: [jQuery] Re: passing args to a delegate


 while this might keep the browser from crashing, I wonder what values
 these parameters will have, when the event finally occurs. The ones I
 have passed the very first time? I thought they would have been
 destroyed by then, as we left this scope long ago ...
 But I'll try it out :), thanks!

 On Sat, Dec 13, 2008 at 3:22 AM, Mike Nichols nichols.mik...@gmail.com 
 wrote:

 Try this:
 success: function() { registerImageForms(id,key,type); }


 On Dec 12, 5:13 pm, Jan Limpens jan.limp...@gmail.com wrote:
 Hello,

 I have the following code:

 var registerImageForms = function(id, key, type) {
 var sizes = ['small', 'medium', 'large'];
 $('#panel-images fieldset:visible').remove();
 $.each(sizes, function(i, item) {
 var $clonedForm = $('#panel-images fieldset:hidden').clone();
 $(legend, $clonedForm).text(item);
 $([name='id'], $clonedForm).val(id);
 $([name='key'], $clonedForm).val(key);
 $([name='type'], $clonedForm).val(type);
 $([name='size'], $clonedForm).val(item);
 $(img, $clonedForm).attr('src', /imagem/article/ + key +
 / + item + .png);
 $(#panel-images).append($clonedForm);
 $(form, $clonedForm).ajaxForm({
 success: registerImageForms
 });
 $clonedForm.show();
 });

 };

 Success has no args, so everything is rendered empty, after posting
 the form. If I pass arguments as
 success: registerImageForms(id, key, type)

 The browser crashes and it makes sense, because at the time this
 fires, these identifiers mean nothing. But how do I pass them?

 --
 Jan



 --
 Jan





-- 
Jan


[jQuery] Re: passing args to a delegate

2008-12-13 Thread Jan Limpens

while this might keep the browser from crashing, I wonder what values
these parameters will have, when the event finally occurs. The ones I
have passed the very first time? I thought they would have been
destroyed by then, as we left this scope long ago ...
But I'll try it out :), thanks!

On Sat, Dec 13, 2008 at 3:22 AM, Mike Nichols nichols.mik...@gmail.com wrote:

 Try this:
 success: function() { registerImageForms(id,key,type); }


 On Dec 12, 5:13 pm, Jan Limpens jan.limp...@gmail.com wrote:
 Hello,

 I have the following code:

 var registerImageForms = function(id, key, type) {
 var sizes = ['small', 'medium', 'large'];
 $('#panel-images fieldset:visible').remove();
 $.each(sizes, function(i, item) {
 var $clonedForm = $('#panel-images fieldset:hidden').clone();
 $(legend, $clonedForm).text(item);
 $([name='id'], $clonedForm).val(id);
 $([name='key'], $clonedForm).val(key);
 $([name='type'], $clonedForm).val(type);
 $([name='size'], $clonedForm).val(item);
 $(img, $clonedForm).attr('src', /imagem/article/ + key +
 / + item + .png);
 $(#panel-images).append($clonedForm);
 $(form, $clonedForm).ajaxForm({
 success: registerImageForms
 });
 $clonedForm.show();
 });

 };

 Success has no args, so everything is rendered empty, after posting
 the form. If I pass arguments as
 success: registerImageForms(id, key, type)

 The browser crashes and it makes sense, because at the time this
 fires, these identifiers mean nothing. But how do I pass them?

 --
 Jan



-- 
Jan


[jQuery] Re: passing args to a delegate

2008-12-13 Thread Jeffrey Kretz

Mike's suggestion involves creating a javascript closure.

There are a number of good articles on closues.

http://www.google.com/search?hl=enq=javascript+closuresrlz=1W1GGLL_en

But essentially, when you create a function that has a reference to a value 
outside of its scope, that function is created as a closure, with its own 
context that has access to those variables, even when they've been changed on 
the global scope.

Try this:

var x = 25;
var fn = function(val){
   return function(){
  alert(val);
   };
}(x);
x = 30;
fn();

The alert will be for 25, rather than 30, as the closure has its own context 
now for the x variable.

The downside to closures is that handled incorrectly, they can cause memory 
leaks.

But review of the many articles on the subject will help keep that from 
happening.

JK


-Original Message-
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On Behalf 
Of Jan Limpens
Sent: Saturday, December 13, 2008 2:28 PM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: passing args to a delegate


while this might keep the browser from crashing, I wonder what values
these parameters will have, when the event finally occurs. The ones I
have passed the very first time? I thought they would have been
destroyed by then, as we left this scope long ago ...
But I'll try it out :), thanks!

On Sat, Dec 13, 2008 at 3:22 AM, Mike Nichols nichols.mik...@gmail.com wrote:

 Try this:
 success: function() { registerImageForms(id,key,type); }


 On Dec 12, 5:13 pm, Jan Limpens jan.limp...@gmail.com wrote:
 Hello,

 I have the following code:

 var registerImageForms = function(id, key, type) {
 var sizes = ['small', 'medium', 'large'];
 $('#panel-images fieldset:visible').remove();
 $.each(sizes, function(i, item) {
 var $clonedForm = $('#panel-images fieldset:hidden').clone();
 $(legend, $clonedForm).text(item);
 $([name='id'], $clonedForm).val(id);
 $([name='key'], $clonedForm).val(key);
 $([name='type'], $clonedForm).val(type);
 $([name='size'], $clonedForm).val(item);
 $(img, $clonedForm).attr('src', /imagem/article/ + key +
 / + item + .png);
 $(#panel-images).append($clonedForm);
 $(form, $clonedForm).ajaxForm({
 success: registerImageForms
 });
 $clonedForm.show();
 });

 };

 Success has no args, so everything is rendered empty, after posting
 the form. If I pass arguments as
 success: registerImageForms(id, key, type)

 The browser crashes and it makes sense, because at the time this
 fires, these identifiers mean nothing. But how do I pass them?

 --
 Jan



-- 
Jan



[jQuery] Re: passing args to a delegate

2008-12-12 Thread Mike Nichols

Try this:
success: function() { registerImageForms(id,key,type); }


On Dec 12, 5:13 pm, Jan Limpens jan.limp...@gmail.com wrote:
 Hello,

 I have the following code:

 var registerImageForms = function(id, key, type) {
     var sizes = ['small', 'medium', 'large'];
     $('#panel-images fieldset:visible').remove();
     $.each(sizes, function(i, item) {
         var $clonedForm = $('#panel-images fieldset:hidden').clone();
         $(legend, $clonedForm).text(item);
         $([name='id'], $clonedForm).val(id);
         $([name='key'], $clonedForm).val(key);
         $([name='type'], $clonedForm).val(type);
         $([name='size'], $clonedForm).val(item);
         $(img, $clonedForm).attr('src', /imagem/article/ + key +
 / + item + .png);
         $(#panel-images).append($clonedForm);
         $(form, $clonedForm).ajaxForm({
             success: registerImageForms
         });
         $clonedForm.show();
     });

 };

 Success has no args, so everything is rendered empty, after posting
 the form. If I pass arguments as
             success: registerImageForms(id, key, type)

 The browser crashes and it makes sense, because at the time this
 fires, these identifiers mean nothing. But how do I pass them?

 --
 Jan