UI-widget destroy() method.

38 views
Skip to first unread message

Tim Toolman

unread,
Dec 12, 2009, 5:47:31 PM12/12/09
to jquery...@googlegroups.com
Hey guys;

I am writing a widget, and I have a question about my destroy() method:

    destroy: function() {
       // revert DOM to original state ...

        this.element.unbind(); // expected $.widget to do this!
       
        $.widget.prototype.destroy.apply(this, arguments); // default destroy
    }

This is my code, with the revert bit removed for clarity. 

I want to call $widgetHandle.mywidget("destroy"); manually, which works, but the events still have references to the widget.  I have to manually  .unbind()  to remove all the events with references that are are stale.  The stale calls happen on remove of the original node, which should now be un-widget-ed.

I expected the default destroy to unbind the events it makes.  Is there a similar issue with widget data left in the .data() space somewhere? I want to be able to generate a completely new widget instance on the same node, so I can't have hangovers from the old widget instance ..

Hope I make sense :)


--
Tim Taylor

Scott González

unread,
Dec 12, 2009, 6:44:47 PM12/12/09
to jquery...@googlegroups.com
The widget factory doesn't unbind event handlers because it doesn't
bind any. You are fully responsible for undoing anything you do.
There's absolutely no way for the factory to know what you've done.

Anything the factory does, it cleans up, which is why you have to call
the base method.
> --
>
> You received this message because you are subscribed to the Google Groups "jQuery UI Development" group.
> To post to this group, send email to jquery...@googlegroups.com.
> To unsubscribe from this group, send email to jquery-ui-de...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jquery-ui-dev?hl=en.
>

Toolman

unread,
Dec 12, 2009, 8:48:52 PM12/12/09
to jQuery UI Development
I disagree; look at jquery.ui.js line 269-285


this.element = $(element)
.bind('setData.' + name, function(event, key, value) {
if (event.target == element) {
return self._setData(key, value);
}
})
.bind('getData.' + name, function(event, key) {
if (event.target == element) {
return self._getData(key);
}
})
.bind('remove', function() {
return self.destroy();
});

that last bind fires even after destroy unless I unbind it.

I totally agree that I am responsible for my own events, but these are
not my widgets responsibility.

On Dec 13, 12:44 pm, Scott González <scott.gonza...@gmail.com> wrote:
> The widget factory doesn't unbind event handlers because it doesn't
> bind any. You are fully responsible for undoing anything you do.
> There's absolutely no way for the factory to know what you've done.
>
> Anything the factory does, it cleans up, which is why you have to call
> the base method.
>

Toolman

unread,
Dec 12, 2009, 8:51:21 PM12/12/09
to jQuery UI Development
I should say; ui.core.js v1.7.2 not jquery.ui.js !

Scott González

unread,
Dec 13, 2009, 10:26:03 AM12/13/09
to jquery...@googlegroups.com
The fact that the remove event isn't namespaced is a bug and was fixed a while ago.

Toolman

unread,
Dec 13, 2009, 10:13:07 PM12/13/09
to jQuery UI Development
I am trying to understand your answer; I'm looking at the trunk
ui.core.js code:

http://jquery-ui.googlecode.com/svn/trunk/ui/jquery.ui.core.js

As I read it, nothing has changed. It is still leaving the "remove"
event handler on "this.element". (currently line 312) The destroy
method (line 323) removes data, classes and attributes, but not the 3
binded event handlers.

Am I not understanding this correctly? Please help me over here in
the cheap seats, because I can't see any fix in the trunk source, and
I think it is a real bug. After I manually destroy my widget, there
are still unwanted events on the original node.

Toolman :)

On Dec 14, 4:26 am, Scott González <scott.gonza...@gmail.com> wrote:
> The fact that the remove event isn't namespaced is a bug and was fixed a
> > jquery-ui-de...@googlegroups.com<jquery-ui-dev%2Bunsu...@googlegroups.com>
> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/jquery-ui-dev?hl=en.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "jQuery UI Development" group.
> > To post to this group, send email to jquery...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > jquery-ui-de...@googlegroups.com<jquery-ui-dev%2Bunsu...@googlegroups.com>
> > .

Toolman

unread,
Dec 13, 2009, 10:23:32 PM12/13/09
to jQuery UI Development
Re-reading the source, I see that the event name has been "namespaced"
by the addition on the "remove." + name suffix, but the problem still
exists; these stale event handlers are not removed on destroy.

If you think they are being removed, can you tell me where it that
happens?

Thanks,

Toolman

Scott González

unread,
Dec 14, 2009, 8:42:56 AM12/14/09
to jquery...@googlegroups.com
You're correct, those aren't being removed and they should. When you first pointed out the event bindings, you specifically pointed out the remove event so I thought it was the namespacing problem that had already been fixed.

I've created a ticket for this: http://dev.jqueryui.com/ticket/5005

Thanks for pointing this out, and sorry for the confusion.

Toolman

unread,
Dec 14, 2009, 4:12:24 PM12/14/09
to jQuery UI Development
Thank you for reviewing and logging this, glad that I wasn't talking
crap :)

On Dec 15, 2:42 am, Scott González <scott.gonza...@gmail.com> wrote:
> You're correct, those aren't being removed and they should. When you first
> pointed out the event bindings, you specifically pointed out the remove
> event so I thought it was the namespacing problem that had already been
> fixed.
>
> I've created a ticket for this:http://dev.jqueryui.com/ticket/5005
>
> Thanks for pointing this out, and sorry for the confusion.
>

Toolman

unread,
Dec 14, 2009, 4:37:04 PM12/14/09
to jQuery UI Development
Thank you for reviewing and logging this, glad that I wasn't talking
crap :)

On Dec 15, 2:42 am, Scott González <scott.gonza...@gmail.com> wrote:
> You're correct, those aren't being removed and they should. When you first
> pointed out the event bindings, you specifically pointed out the remove
> event so I thought it was the namespacing problem that had already been
> fixed.
>
> I've created a ticket for this:http://dev.jqueryui.com/ticket/5005
>
> Thanks for pointing this out, and sorry for the confusion.
>
Reply all
Reply to author
Forward
0 new messages