Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add destroy method #59

Merged
merged 3 commits into from
Feb 28, 2013
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Dataset = (function() {
this.limit = o.limit || 10;
this._customMatcher = o.matcher || null;
this._customRanker = o.ranker || null;
this._ttl_ms = o.ttl_ms || 3 * 24 * 60 * 60 * 1000; // 3 days;
this._ttl_ms = utils.isNumber(o.ttl_ms) ? o.ttl_ms : 24 * 60 * 60 * 1000;

this.keys = {
version: 'version',
Expand Down
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a suffix instead of a prefix? Does this.$menu.off('.tt'); not work with prefixes? Just curious because I haven't seen this before…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the link.

.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();
}
})();