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

Hide headers and footers when the dataset contains no suggestions #113

Merged
Show file tree
Hide file tree
Changes from all 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
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