Background on jQuery live:
-------------------------
The classic way to bind to an event is doing something like:
jQuery("selector").click(function() { 
        // do some stuff
});
//Note this is just a wrapper to jQuery("...").bind("click", ...);

The issue with that method is that it statically binds the event to whatever 
elements match your selector AT THE TIME YOU CALL .click(). So if new elements 
are dynamically added to the page using javascript, the event handler won't be 
attached to those new elements. jQuery also has the .live function, where 
events can be bound as follows:
jQuery("selector").live("click", function() {
        // do some stuff
}
Using .live(), the click event is now dynamically attached to all elements that 
match selector, regardless of the time they are added to the page.

The issue I was having:
------------------------------
So in my example, since web2py initializes its form event handlers with .bind, 
when I load a new form onto the page via ajax then it doesn't have any of the 
event handlers attached to it. This means the date fields are all static, 
delete checkboxes have no confirmation, etc.

What are thoughts on attaching the events using .live() instead. The only 
drawback to .live() I can think of is the increase in performance cost required 
to support dynamic events.

If you want to make it the new default, I'll submit patch.

-----Original Message-----
From: web2py@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of 
mdipierro
Sent: Wednesday, November 24, 2010 12:51 PM
To: web2py-users
Subject: [web2py] Re: targeted web2py_ajax_init for dynamically inserted forms

tell us more about jquery live. What do you propose?

On Nov 24, 11:44 am, Josh Jaques <jjaq...@seccuris.com> wrote:
> If you load a form onto a web2py page via AJAX, it doesn't get the 
> enhancements from web2py_ajax_init().
>
> I've included a modified web2py_ajax_init that accepts an optional parent 
> selector to provide some scope to web2py_ajax_init. In this way if you load a 
> form onto a page, you can call web2py_ajax_init on that specific form to give 
> it the enhancements, without effecting every other form on the page.
>
> I've included the modified function here as it may be useful to others in the 
> future.
>
> An alternative approach may be to bind events using jQuery.live(), so that 
> you don't have to explicitly call web2py_ajax_init() every time you load a 
> form.
>
> --------
> function web2py_ajax_ init(parent) {
>   var find = function(selector) {
>       if (parent)
>           return $(parent).find(selector);
>       else
>           return jQuery(selector);
>   }
>   find('.hidden').hide();
>   find('.error').hide().slideDown('slow');
>   find('.flash').click(function() { find(this).fadeOut('slow'); return false; 
> });
>   // find('input[type=submit]').click(function(){var 
> t=find(this);t.hide();t.after('<input class="submit_disabled" 
> disabled="disabled" type="submit" name="'+t.attr("name")+'_dummy" 
> value="'+t.val()+'">')});
>   
> find('input.integer').keyup(function(){this.value=this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g,'').reverse();});
>   
> find('input.double,input.decimal').keyup(function(){this.value=this.value.reverse().replace(/[^0-9\-\.]|[\-](?=.)|[\.](?=[0-9]*[\.])/g,'').reverse();});
>   
> find("input[type='checkbox'].delete").each(function(){find(this).click(function()
>  { if(this.checked) if(!confirm("{{=T('Sure you want to delete this 
> object?')}}")) this.checked=false; });});
>   try {find("input.date").focus(function() {Calendar.setup({
>      inputField:this.id, ifFormat:"{{=T('%Y-%m-%d')}}", showsTime:false
>   }); }); } catch(e) {};
>   try { find("input.datetime").focus( function() {Calendar.setup({
>      inputField:this.id, ifFormat:"{{=T('%Y-%m-%d %H:%M:%S')}}", showsTime: 
> true,timeFormat: "24"
>   }); }); } catch(e) {};
>   try { find("input.time").timeEntry(); } catch(e) {};};
>
> ---------------
>
> This communication, including any attachments, does not necessarily represent 
> official policy of Seccuris Inc.
> Please seehttp://www.seccuris.com/Contact-PrivacyPolicy.htm for further 
> details about Seccuris Inc.'s Privacy Policy.
> If you have received this communication in error, please notify Seccuris Inc. 
> at i...@seccuris.com or at 1-866-644-8442.
This communication, including any attachments, does not necessarily represent 
official policy of Seccuris Inc.
Please see http://www.seccuris.com/Contact-PrivacyPolicy.htm  for further 
details about Seccuris Inc.'s Privacy Policy.
If you have received this communication in error, please notify Seccuris Inc. 
at i...@seccuris.com or at 1-866-644-8442.

Reply via email to