From 101d81ce129d08088af6930c72109d1de30ef633 Mon Sep 17 00:00:00 2001 From: Jake Harding Date: Wed, 13 Mar 2013 09:04:10 -0700 Subject: [PATCH] Hide headers and footers when the dataset contains no suggestions. --- src/dropdown_view.js | 32 +++++++++++++++++++++----------- test/dropdown_view_spec.js | 12 ++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/dropdown_view.js b/src/dropdown_view.js index ae9b1155..5d32ebf7 100644 --- a/src/dropdown_view.js +++ b/src/dropdown_view.js @@ -197,15 +197,14 @@ var DropdownView = (function() { .appendTo(this.$menu); } - elBuilder = document.createElement('div'); - fragment = document.createDocumentFragment(); - - this.clearSuggestions(dataset.name); - + // suggestions to be rendered if (suggestions.length > 0) { this.isEmpty = false; this.isOpen && this._show(); + elBuilder = document.createElement('div'); + fragment = document.createDocumentFragment(); + utils.each(suggestions, function(i, suggestion) { elBuilder.innerHTML = dataset.template.render(suggestion); @@ -219,20 +218,31 @@ var DropdownView = (function() { fragment.appendChild($el[0]); }); + + // show this dataset in case it was previously empty + // and render the new suggestions + $dataset + .show() + .find('.tt-suggestions') + .data({ query: query, dataset: dataset.name }) + .html(fragment); } - $dataset.find('> .tt-suggestions') - .data({ query: query, dataset: dataset.name }) - .append(fragment); + // no suggestions to render + else { + this.clearSuggestions(dataset.name); + } this.trigger('suggestionsRendered'); }, clearSuggestions: function(datasetName) { - var $suggestions = datasetName ? - this.$menu.find('.tt-dataset-' + datasetName + ' .tt-suggestions') : - this.$menu.find('.tt-suggestions'); + var $datasets = datasetName ? + this.$menu.find('.tt-dataset-' + datasetName) : + this.$menu.find('[class^="tt-dataset-"]'), + $suggestions = $datasets.find('.tt-suggestions'); + $datasets.hide(); $suggestions.empty(); if (this._getSuggestions().length === 0) { diff --git a/test/dropdown_view_spec.js b/test/dropdown_view_spec.js index 7348f1cc..f8d4f5e2 100644 --- a/test/dropdown_view_spec.js +++ b/test/dropdown_view_spec.js @@ -504,8 +504,8 @@ describe('DropdownView', function() { describe('if there are no suggestions', function() { beforeEach(function() { - this.dropdownView. - on('suggestionsRendered', this.spy = jasmine.createSpy()); + this.dropdownView + .on('suggestionsRendered', this.spy = jasmine.createSpy()); spyOn(this.dropdownView, 'clearSuggestions'); @@ -535,10 +535,6 @@ describe('DropdownView', function() { ); }); - it('should call clearSuggestions', function() { - expect(this.dropdownView.clearSuggestions).toHaveBeenCalledWith('test'); - }); - it('should update data values on list', function() { expect(this.$testDataset).toHaveData('query', 'query'); expect(this.$testDataset).toHaveData('dataset', 'test'); @@ -564,6 +560,10 @@ describe('DropdownView', function() { this.dropdownView.clearSuggestions(); }); + it('should hide all datasets', function() { + expect(this.$menu.find('[class^="tt-dataset-"]')).toBeHidden(); + }); + it('should remove all suggestions', function() { expect(this.$menu.find('.tt-suggestion')).not.toExist(); });