Skip to content

Commit d3bdb0a

Browse files
author
Tomas Kirda
committed
Add dispose method. Remove unused code. Destroy existing instance when creating new instance. Fixes devbridge#55.
1 parent 97e421c commit d3bdb0a

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/jquery.autocomplete.js

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,6 @@
3131
return $.extend(target, source);
3232
},
3333

34-
addEvent: function (element, eventType, handler) {
35-
if (element.addEventListener) {
36-
element.addEventListener(eventType, handler, false);
37-
} else if (element.attachEvent) {
38-
element.attachEvent('on' + eventType, handler);
39-
} else {
40-
throw new Error('Browser doesn\'t support addEventListener or attachEvent');
41-
}
42-
},
43-
44-
removeEvent: function (element, eventType, handler) {
45-
if (element.removeEventListener) {
46-
element.removeEventListener(eventType, handler, false);
47-
} else if (element.detachEvent) {
48-
element.detachEvent('on' + eventType, handler);
49-
}
50-
},
51-
5234
createNode: function (html) {
5335
var div = document.createElement('div');
5436
div.innerHTML = html;
@@ -168,33 +150,33 @@
168150
container.appendTo(options.appendTo).width(options.width);
169151

170152
// Listen for mouse over event on suggestions list:
171-
container.on('mouseover', suggestionSelector, function () {
153+
container.on('mouseover.autocomplete', suggestionSelector, function () {
172154
that.activate($(this).data('index'));
173155
});
174156

175157
// Deselect active element when mouse leaves suggestions container:
176-
container.on('mouseout', function () {
158+
container.on('mouseout.autocomplete', function () {
177159
that.selectedIndex = -1;
178160
container.children('.' + selected).removeClass(selected);
179161
});
180162

181163
// Listen for click event on suggestions list:
182-
container.on('click', suggestionSelector, function () {
164+
container.on('click.autocomplete', suggestionSelector, function () {
183165
that.select($(this).data('index'), false);
184166
});
185167

186168
that.fixPosition();
187169

188170
// Opera does not like keydown:
189171
if (window.opera) {
190-
that.el.on('keypress', function (e) { that.onKeyPress(e); });
172+
that.el.on('keypress.autocomplete', function (e) { that.onKeyPress(e); });
191173
} else {
192-
that.el.on('keydown', function (e) { that.onKeyPress(e); });
174+
that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); });
193175
}
194176

195-
that.el.on('keyup', function (e) { that.onKeyUp(e); });
196-
that.el.on('blur', function () { that.onBlur(); });
197-
that.el.on('focus', function () { that.fixPosition(); });
177+
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
178+
that.el.on('blur.autocomplete', function () { that.onBlur(); });
179+
that.el.on('focus.autocomplete', function () { that.fixPosition(); });
198180
},
199181

200182
onBlur: function () {
@@ -259,12 +241,12 @@
259241

260242
enableKillerFn: function () {
261243
var that = this;
262-
$(document).on('click', that.killerFn);
244+
$(document).on('click.autocomplete', that.killerFn);
263245
},
264246

265247
disableKillerFn: function () {
266248
var that = this;
267-
$(document).off('click', that.killerFn);
249+
$(document).off('click.autocomplete', that.killerFn);
268250
},
269251

270252
killSuggestions: function () {
@@ -617,6 +599,13 @@
617599
}
618600

619601
return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
602+
},
603+
604+
dispose: function() {
605+
var that = this;
606+
that.el.off('.autocomplete').removeData('autocomplete');
607+
that.disableKillerFn();
608+
$(that.suggestionsContainer).remove();
620609
}
621610
};
622611

@@ -625,14 +614,17 @@
625614
return this.each(function () {
626615
var dataKey = 'autocomplete',
627616
inputElement = $(this),
628-
instance;
617+
instance = inputElement.data(dataKey);
629618

630619
if (typeof options === 'string') {
631-
instance = inputElement.data(dataKey);
632-
if (typeof instance[options] === 'function') {
620+
if (instance && typeof instance[options] === 'function') {
633621
instance[options](args);
634622
}
635623
} else {
624+
// If instance already exists, destroy it:
625+
if (instance && instance.dispose) {
626+
instance.dispose();
627+
}
636628
instance = new Autocomplete(this, options);
637629
inputElement.data(dataKey, instance);
638630
}

0 commit comments

Comments
 (0)