Skip to content

Commit

Permalink
Merge pull request #113 from jharding/hide-headers-and-footers-when-n…
Browse files Browse the repository at this point in the history
…o-suggestions

Hide headers and footers when the dataset contains no suggestions
  • Loading branch information
jharding committed Mar 13, 2013
2 parents d425204 + 101d81c commit 16e888c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
32 changes: 21 additions & 11 deletions src/dropdown_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions test/dropdown_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');
Expand All @@ -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();
});
Expand Down

0 comments on commit 16e888c

Please sign in to comment.