Skip to content

Commit

Permalink
Add destroy plugin method for destroying typeahead instances. Fixes #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Feb 27, 2013
1 parent eb417ea commit 126e780
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/js/dropdown_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var DropdownView = (function() {
this.isMouseOverDropdown;

this.$menu = $(o.menu)
.on('mouseenter', this._handleMouseenter)
.on('mouseleave', this._handleMouseleave)
.on('mouseover', '.tt-suggestions > .tt-suggestion', this._handleMouseover)
.on('click', '.tt-suggestions > .tt-suggestion', this._handleSelection);
.on('mouseenter.tt', this._handleMouseenter)
.on('mouseleave.tt', this._handleMouseleave)
.on('click.tt', '.tt-suggestion', this._handleSelection)
.on('mouseover.tt', '.tt-suggestion', this._handleMouseover);
}

utils.mixin(DropdownView.prototype, EventTarget, {
Expand Down Expand Up @@ -81,6 +81,12 @@ var DropdownView = (function() {
// public methods
// --------------

destroy: function() {
this.$menu.off('.tt');

this.$menu = null;
},

hideUnlessMouseIsOverDropdown: function() {
// this helps detect the scenario a blur event has triggered
// this function. we don't want to hide the menu in that case
Expand Down
17 changes: 12 additions & 5 deletions src/js/input_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ var InputView = (function() {

this.$hint = $(o.hint);
this.$input = $(o.input)
.on('blur', this._handleBlur)
.on('focus', this._handleFocus)
.on('keydown', this._handleSpecialKeyEvent);
.on('blur.tt', this._handleBlur)
.on('focus.tt', this._handleFocus)
.on('keydown.tt', this._handleSpecialKeyEvent);

// ie7 and ie8 don't support the input event
// ie9 doesn't fire the input event when characters are removed
// not sure if ie10 is compatible
if (!utils.isMsie()) {
this.$input.on('input', this._compareQueryToInputValue);
this.$input.on('input.tt', this._compareQueryToInputValue);
}

else {
this.$input
.on('keydown keypress cut paste', function(e) {
.on('keydown.tt keypress.tt cut.tt paste.tt', function(e) {
// if a special key triggered this, ignore it
if (that.specialKeyCodeMap[e.which || e.keyCode]) { return; }

Expand Down Expand Up @@ -92,6 +92,13 @@ var InputView = (function() {
// public methods
// --------------

destroy: function() {
this.$hint.off('.tt');
this.$input.off('.tt');

this.$hint = this.$input = this.$overflowHelper = null;
},

focus: function() {
this.$input.focus();
},
Expand Down
8 changes: 8 additions & 0 deletions src/js/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
typeahead: new TypeaheadView({ input: this, datasets: datasets })
});
});
},

destroy: function() {
this.each(function() {
var view = $(this).data('typeahead');

view && view.destroy();
});
}
};

Expand Down
34 changes: 34 additions & 0 deletions src/js/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ var TypeaheadView = (function() {
if (hint !== '' && query !== hint) {
this.inputView.setInputValue(hint);
}
},

// public methods
// --------------

destroy: function() {
this.inputView.destroy();
this.dropdownView.destroy();

destroyDomStructure(this.$node);

this.$node = null;
}
});

Expand All @@ -217,6 +229,14 @@ var TypeaheadView = (function() {
return null;
}

// store the original values of the attrs that get modified
// so modifications can be reverted on destroy
$input.data('ttAttrs', {
dir: $input.attr('dir'),
autocomplete: $input.attr('autocomplete'),
spellcheck: $input.attr('spellcheck')
});

// ie7 does not like it when dir is set to auto,
// it does not like it one bit
try { !$input.attr('dir') && $input.attr('dir', 'auto'); } catch (e) {}
Expand All @@ -229,4 +249,18 @@ var TypeaheadView = (function() {
.prepend($hint)
.append(html.dropdown);
}

function destroyDomStructure($node) {
var $input = $node.find('.tt-query');

// need to remove attrs that weren't previously defined and
// revert attrs that originally had a value
utils.each($input.data('ttAttrs'), function(key, val) {
utils.isUndefined(val) ? $input.removeAttr(key) : $input.attr(key, val);
});

$input.detach().removeClass('tt-query').insertAfter($node);

$node.remove();
}
})();

0 comments on commit 126e780

Please sign in to comment.