diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index 6d809c90c..35365574d 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -256,9 +256,11 @@ _.mixin(Dataset.prototype, EventEmitter, { }, clear: function clear() { - this.cancel(); - this.$el.empty(); - this.trigger('rendered', ''); + if (this.$el) { + this.cancel(); + this.$el.empty(); + this.trigger('rendered', ''); + } }, isEmpty: function isEmpty() { diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index 0e853a30d..1bec373b1 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -462,6 +462,14 @@ describe('Dataset', function() { this.dataset.clear(); expect(spy).toHaveBeenCalled(); }); + + it('should not throw if called right after destroy()', function() { + this.source.and.callFake(fakeGetWithSyncResults); + this.dataset.update('woah'); + this.dataset.destroy(); + this.dataset.clear(); + expect(this.dataset.getRoot()).toBeNull(); + }); }); describe('#isEmpty', function() {