wiring to .live()

12 views
Skip to first unread message

Jeff Bell

unread,
Feb 7, 2012, 9:59:09 PM2/7/12
to Zotonic users
There was a recent post on SO that distinguished the differences of .click .bind etc in JQuery.  In the post, it was mentioned that using .live can be a performance improvement, especially if you have 1000's of .click() events registered on one page load.


So, I was wondering if there was a canonical way to use {% wire %} to observe .live('click', function() {... . 

Here's what I'm trying to replace:
 
{% wire id=["add_request_", node_id]|join action={ update target="modal" template="_customer_node_request.tpl" id=id node_id=node_id }
                                                            action={mymodal height="500" width="500"}
%}

But this binds the click() function to all the items, which slows down page load.

I've tried this as well...
<script>
$(function(){
    $('.add_request').live('click', function () { 
        $("#modal").load( {% url customer_node_request_new id=id node_id=node_id %} );
         mycustom_modal(500,500);
    });   
});
</script>
But that doesn't initialize the scripts on the newly loaded template, so postbacks for example don't work.

I'd appreciate for any hint to examples in the source code.  There may be some typos above, because I'm typing this from memory.

Cheers,

Jeff

Andreas Stenius

unread,
Feb 8, 2012, 2:11:10 AM2/8/12
to zotoni...@googlegroups.com
The easiest I could come up with was to patch the scomp & action
involved with the wire & bind:

See my branch here (UNTESTED CODE):
https://github.com/kaos/zotonic/compare/master...jquery-live

You should be able to use it like this:

{% wire id=["add_request_", node_id]|join

event_method="live"


action={ update target="modal"
template="_customer_node_request.tpl" id=id node_id=node_id }
action={mymodal height="500" width="500"}
%}

While reading the jQuery docs, I notice that .live() is deprecated in
favor for .delegate(), or, from v1.7, .on().

So, I'd say we should update the jQuery lib in zotonic, and migrate
the events to use the new and improved .on() event attach method.

Let me know how it goes!

Cheers,
Andreas

2012/2/8 Jeff Bell <jeff....@gmail.com>:

Jeff Bell

unread,
Feb 8, 2012, 3:46:34 PM2/8/12
to zotoni...@googlegroups.com
Andreas,

Thanks for sending me this link.  I'll check it out and let you know how it turns out.

Jeff
Reply all
Reply to author
Forward
0 new messages