fgelinas / timepicker

A jQuery UI Time Picker
http://fgelinas.com/code/timepicker
GNU General Public License v2.0
266 stars 84 forks source link

Provide 'remove' option #59

Open adam-lynch opened 11 years ago

adam-lynch commented 11 years ago

$('#timepicker-input').timepicker('remove') would be nice.

I was troubleshooting an issue where we having where we add inputs to the DOM, initialize the timepicker on them, then remove them from the DOM, then add some other inputs to the DOM, initialize the timpicker on them... then the timepicker would show but a click on a date wouldn't do anything.

That isn't the reason for this issue. I decided to completely remove the timepicker when the inputs are removed from the DOM*, and the best way I found to achieve this was the following:

//remove timepicker div
$($.timepicker._tpDiv).remove();

//unbind all related events
$($elem).find('.has-timepicker').unbind('.timepicker');

/*
$.fn.timepicker is subscribed to the mousedown event for the whole document (default event; no namespace)
so we can't undo that but when that event is triggered they first check if there's an instance of the
timepicker ($.timepicker._curInst)
*/

//reset timepicker settings; tell it there's no timepicker instance
$.timepicker._curInst = false;

It would've been much nicer if I could've just had $($elem).find('.has-timepicker').timepicker('remove') or (because the former is per field) $.timepicker.remove() to completely remove the timepicker instance and event handling.

* - well, when the modal they're in is hidden actually (the hidden modal will never be used again, it will most likely be replaced by another modal containing other inputs)

fgelinas commented 11 years ago

Hi, There is actually a "destroy" option that remove the timepicker classes from the input and unbind events : $($elem).timepicker('destroy'); should work. It will not unset the _curInst because the function _destroyTimepicker function does not check if it is currently showing. However if the user click a close button or something outside a showing timepicker, The _hideTimepicker will be called and the _curInst will be set to null.

adam-lynch commented 11 years ago

Sorry, must've missed that in the documentation.

Sounds good. I'll swap out my code for this next week and let you know if anything unexpected happens.