|
31 | 31 | return $.extend(target, source); |
32 | 32 | }, |
33 | 33 |
|
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 | | - |
52 | 34 | createNode: function (html) { |
53 | 35 | var div = document.createElement('div'); |
54 | 36 | div.innerHTML = html; |
|
168 | 150 | container.appendTo(options.appendTo).width(options.width); |
169 | 151 |
|
170 | 152 | // Listen for mouse over event on suggestions list: |
171 | | - container.on('mouseover', suggestionSelector, function () { |
| 153 | + container.on('mouseover.autocomplete', suggestionSelector, function () { |
172 | 154 | that.activate($(this).data('index')); |
173 | 155 | }); |
174 | 156 |
|
175 | 157 | // Deselect active element when mouse leaves suggestions container: |
176 | | - container.on('mouseout', function () { |
| 158 | + container.on('mouseout.autocomplete', function () { |
177 | 159 | that.selectedIndex = -1; |
178 | 160 | container.children('.' + selected).removeClass(selected); |
179 | 161 | }); |
180 | 162 |
|
181 | 163 | // Listen for click event on suggestions list: |
182 | | - container.on('click', suggestionSelector, function () { |
| 164 | + container.on('click.autocomplete', suggestionSelector, function () { |
183 | 165 | that.select($(this).data('index'), false); |
184 | 166 | }); |
185 | 167 |
|
186 | 168 | that.fixPosition(); |
187 | 169 |
|
188 | 170 | // Opera does not like keydown: |
189 | 171 | if (window.opera) { |
190 | | - that.el.on('keypress', function (e) { that.onKeyPress(e); }); |
| 172 | + that.el.on('keypress.autocomplete', function (e) { that.onKeyPress(e); }); |
191 | 173 | } else { |
192 | | - that.el.on('keydown', function (e) { that.onKeyPress(e); }); |
| 174 | + that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); }); |
193 | 175 | } |
194 | 176 |
|
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(); }); |
198 | 180 | }, |
199 | 181 |
|
200 | 182 | onBlur: function () { |
|
259 | 241 |
|
260 | 242 | enableKillerFn: function () { |
261 | 243 | var that = this; |
262 | | - $(document).on('click', that.killerFn); |
| 244 | + $(document).on('click.autocomplete', that.killerFn); |
263 | 245 | }, |
264 | 246 |
|
265 | 247 | disableKillerFn: function () { |
266 | 248 | var that = this; |
267 | | - $(document).off('click', that.killerFn); |
| 249 | + $(document).off('click.autocomplete', that.killerFn); |
268 | 250 | }, |
269 | 251 |
|
270 | 252 | killSuggestions: function () { |
|
617 | 599 | } |
618 | 600 |
|
619 | 601 | 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(); |
620 | 609 | } |
621 | 610 | }; |
622 | 611 |
|
|
625 | 614 | return this.each(function () { |
626 | 615 | var dataKey = 'autocomplete', |
627 | 616 | inputElement = $(this), |
628 | | - instance; |
| 617 | + instance = inputElement.data(dataKey); |
629 | 618 |
|
630 | 619 | if (typeof options === 'string') { |
631 | | - instance = inputElement.data(dataKey); |
632 | | - if (typeof instance[options] === 'function') { |
| 620 | + if (instance && typeof instance[options] === 'function') { |
633 | 621 | instance[options](args); |
634 | 622 | } |
635 | 623 | } else { |
| 624 | + // If instance already exists, destroy it: |
| 625 | + if (instance && instance.dispose) { |
| 626 | + instance.dispose(); |
| 627 | + } |
636 | 628 | instance = new Autocomplete(this, options); |
637 | 629 | inputElement.data(dataKey, instance); |
638 | 630 | } |
|
0 commit comments