2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-09 23:01:00 +00:00

Add dispose method. Remove unused code. Destroy existing instance when creating new instance. Fixes #55.

This commit is contained in:
Tomas Kirda 2013-04-24 14:07:03 -05:00
parent 97e421cf95
commit d3bdb0a61b

View File

@ -31,24 +31,6 @@
return $.extend(target, source);
},
addEvent: function (element, eventType, handler) {
if (element.addEventListener) {
element.addEventListener(eventType, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventType, handler);
} else {
throw new Error('Browser doesn\'t support addEventListener or attachEvent');
}
},
removeEvent: function (element, eventType, handler) {
if (element.removeEventListener) {
element.removeEventListener(eventType, handler, false);
} else if (element.detachEvent) {
element.detachEvent('on' + eventType, handler);
}
},
createNode: function (html) {
var div = document.createElement('div');
div.innerHTML = html;
@ -168,18 +150,18 @@
container.appendTo(options.appendTo).width(options.width);
// Listen for mouse over event on suggestions list:
container.on('mouseover', suggestionSelector, function () {
container.on('mouseover.autocomplete', suggestionSelector, function () {
that.activate($(this).data('index'));
});
// Deselect active element when mouse leaves suggestions container:
container.on('mouseout', function () {
container.on('mouseout.autocomplete', function () {
that.selectedIndex = -1;
container.children('.' + selected).removeClass(selected);
});
// Listen for click event on suggestions list:
container.on('click', suggestionSelector, function () {
container.on('click.autocomplete', suggestionSelector, function () {
that.select($(this).data('index'), false);
});
@ -187,14 +169,14 @@
// Opera does not like keydown:
if (window.opera) {
that.el.on('keypress', function (e) { that.onKeyPress(e); });
that.el.on('keypress.autocomplete', function (e) { that.onKeyPress(e); });
} else {
that.el.on('keydown', function (e) { that.onKeyPress(e); });
that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); });
}
that.el.on('keyup', function (e) { that.onKeyUp(e); });
that.el.on('blur', function () { that.onBlur(); });
that.el.on('focus', function () { that.fixPosition(); });
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
that.el.on('blur.autocomplete', function () { that.onBlur(); });
that.el.on('focus.autocomplete', function () { that.fixPosition(); });
},
onBlur: function () {
@ -259,12 +241,12 @@
enableKillerFn: function () {
var that = this;
$(document).on('click', that.killerFn);
$(document).on('click.autocomplete', that.killerFn);
},
disableKillerFn: function () {
var that = this;
$(document).off('click', that.killerFn);
$(document).off('click.autocomplete', that.killerFn);
},
killSuggestions: function () {
@ -617,6 +599,13 @@
}
return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
},
dispose: function() {
var that = this;
that.el.off('.autocomplete').removeData('autocomplete');
that.disableKillerFn();
$(that.suggestionsContainer).remove();
}
};
@ -625,14 +614,17 @@
return this.each(function () {
var dataKey = 'autocomplete',
inputElement = $(this),
instance;
instance = inputElement.data(dataKey);
if (typeof options === 'string') {
instance = inputElement.data(dataKey);
if (typeof instance[options] === 'function') {
if (instance && typeof instance[options] === 'function') {
instance[options](args);
}
} else {
// If instance already exists, destroy it:
if (instance && instance.dispose) {
instance.dispose();
}
instance = new Autocomplete(this, options);
inputElement.data(dataKey, instance);
}