Skip to content

Commit

Permalink
Merge pull request #1186 from jharding/970-null-destroy
Browse files Browse the repository at this point in the history
Prevent reference errors after destroy
  • Loading branch information
jharding committed Apr 27, 2015
2 parents 330f249 + 8d3e6de commit e40ecd9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/typeahead/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ var Dataset = (function() {
},

destroy: function destroy() {
this.$el = null;
// #970
this.$el = $('<div>');
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/typeahead/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ var Input = (function() {
this.$input.off('.tt');
this.$overflowHelper.remove();

this.$hint = this.$input = this.$overflowHelper = null;
// #970
this.$hint = this.$input = this.$overflowHelper = $('<div>');
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/typeahead/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ var Menu = (function() {
destroy: function destroy() {
this.$node.off('.tt');

this.$node = null;
// #970
this.$node = $('<div>');

_.each(this.datasets, destroyDataset);

Expand Down
7 changes: 4 additions & 3 deletions test/typeahead/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,11 @@ describe('Dataset', function() {
});

describe('#destroy', function() {
it('should null out the reference to the dataset element', function() {
this.dataset.destroy();
it('should set dataset element to dummy element', function() {
var $prevEl = this.dataset.$el;

expect(this.dataset.$el).toBeNull();
this.dataset.destroy();
expect(this.dataset.$el).not.toBe($prevEl);
});
});

Expand Down
14 changes: 10 additions & 4 deletions test/typeahead/input_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,18 @@ describe('Input', function() {
expect($input.off).toHaveBeenCalledWith('.tt');
});

it('should null out its reference to DOM elements', function() {
it('should set references to DOM elements to dummy element', function() {
var $hint, $input, $overflowHelper;

$hint = this.view.$hint;
$input = this.view.$input;
$overflowHelper = this.view.$overflowHelper;

this.view.destroy();

expect(this.view.$hint).toBeNull();
expect(this.view.$input).toBeNull();
expect(this.view.$overflowHelper).toBeNull();
expect(this.view.$hint).not.toBe($hint);
expect(this.view.$input).not.toBe($input);
expect(this.view.$overflowHelper).not.toBe($overflowHelper);
});
});

Expand Down
6 changes: 4 additions & 2 deletions test/typeahead/results_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ describe('Menu', function() {
expect(this.dataset.destroy).toHaveBeenCalled();
});

it('should null out its reference to the node element', function() {
it('should set node element to dummy element', function() {
var $node = this.view.$node;

this.view.destroy();
expect(this.view.$node).toBeNull();
expect(this.view.$node).not.toBe($node);
});
});
});

0 comments on commit e40ecd9

Please sign in to comment.