Ahhh... I see, that's pretty useful.

I think I would be inclined to use the following naming convention:
- copyEvents
- copyEventsTo
- cloneWithEvents

This way "clone" is always associated with cloning elements.
Additionally, adding in the "From" seems sort of redundant (it's not
used elsewhere in jQuery (.appendFrom?)

Finally, now that I see what your goal is, I think this would be an
interesting goal:

$("#foo").load("some.html",function(){
  $(this).copyEvents( true, "#otherfoo" );
});

And this would do a deep traversal of the contents of #foo, attempting
to copy over events from similar elements in #otherfoo.

Of course, an ultimate goal would be:

$("a").click(...);
$("li").addClass("test");

$("#foo").liveLoad( "foo.html" );

and having all the a elements receive that particular event handler,
and all li elements receive a class of test.

Of course, the technical aspects behind this are quite daunting, but
it would certainly be a cool effect.

--John

On 1/25/07, Brandon Aaron <[EMAIL PROTECTED]> wrote:
> Duh! I'll add the cloneWithEvents() method to the plugin shortly. I
> was thinking how to make the plugin as flexible as possible and allow
> for other use-cases instead of just with clone. Lets say for example
> that you have an unordered list and each list item has events. Then
> you add some more list items via AJAX but need to rebind the events.
> Just use the cloneEventsFrom method.
>
> Thanks John ... I believe that completes this plugin.
>
> --
> Brandon Aaron
>
> On 1/25/07, John Resig <[EMAIL PROTECTED]> wrote:
> > I think this is a little different from what was originally asked for.
> > Something along the lines of:
> >
> > jQuery.fn.cloneWithEvents = function( deep ) {
> >   var old = this;
> >   return this.clone( deep ).cloneEventsFrom( old );
> > };
> >
> > This way you can do the cloning (and the event cloning) all in one 
> > operation:
> >
> > $("div.toggle").cloneWithEvents().appendTo("#container");
> >
> > --John
> >
> > On 1/25/07, Brandon Aaron <[EMAIL PROTECTED]> wrote:
> > > I've noticed several times on the list people asking why .clone()
> > > doesn't clone the events along with the elements. No more worries ...
> > > now you can clone those events if you need too!
> > >
> > > var $elements = $('.elements');
> > > $elements
> > >     .clone()
> > >     .cloneEventsFrom($elements);
> > >
> > > There is also a .cloneEventsTo() which acts like .appendTo().
> > >
> > > var $elements = $('.elements');
> > > var $clonedElements = $elements.clone();
> > > $elements.cloneEventsTo($cloneElements);
> > >
> > > You can find this plugin in SVN:
> > > http://jquery.com/dev/svn/trunk/plugins/cloneEvents/cloneEvents.js?format=txt
> > >
> > > You can run the unit tests here:
> > > http://brandon.jquery.com/plugins/cloneEvents/test/unit.html
> > >
> > > --
> > > Brandon Aaron
> > >
> > > _______________________________________________
> > > jQuery mailing list
> > > [email protected]
> > > http://jquery.com/discuss/
> > >
> >
> > _______________________________________________
> > jQuery mailing list
> > [email protected]
> > http://jquery.com/discuss/
> >
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to