Skip to content

Commit

Permalink
Trigger typeahead:opened and typeahead:closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Mar 11, 2013
1 parent daf000f commit 04d8f2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var TypeaheadView = (function() {
.on('cursorRemoved', this._updateHint)
.on('suggestionsRendered', this._updateHint)
.on('opened', this._updateHint)
.on('closed', this._clearHint);
.on('closed', this._clearHint)
.on('opened closed', this._propagateEvent);

this.inputView = new InputView({ input: $input, hint: $hint })
.on('focused', this._openDropdown)
Expand Down Expand Up @@ -261,6 +262,10 @@ var TypeaheadView = (function() {
}
},

_propagateEvent: function(e) {
this.eventBus.trigger(e.type);
},

// public methods
// --------------

Expand Down
7 changes: 6 additions & 1 deletion test/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@
"Wyoming"
]
})
.on('typeahead:selected', logToTextarea);
.on([
'typeahead:selected',
'typeahead:opened',
'typeahead:closed'
].join(' '), logToTextarea);

$('.bad-tokens').typeahead({
local: [
Expand Down Expand Up @@ -147,6 +151,7 @@
stringifiedArgs = JSON ? JSON.stringify(args) : '';

$textarea.val([val, type, stringifiedArgs, '\n'].join('\n'));
$textarea[0].scrollTop = $textarea[0].scrollHeight;
}
</script>
</body>
Expand Down
18 changes: 18 additions & 0 deletions test/typeahead_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,32 @@ describe('TypeaheadView', function() {
_updateHintSpecHelper('dropdownView', 'suggestionsRendered');
});

describe('when dropdownView triggers opened', function() {
beforeEach(function() {
this.spy = spyOnEvent(this.$input, 'typeahead:opened');
this.dropdownView.trigger('opened');
});

// TODO: test _updateHint path

it('should trigger typeahead:opened on the input', function() {
expect(this.spy).toHaveBeenTriggered();
});
});

describe('when dropdownView triggers closed', function() {
beforeEach(function() {
this.spy = spyOnEvent(this.$input, 'typeahead:closed');
this.dropdownView.trigger('closed');
});

it('should clear hint', function() {
expect(this.inputView.setHintValue).toHaveBeenCalledWith('');
});

it('should trigger typeahead:closed on the input', function() {
expect(this.spy).toHaveBeenTriggered();
});
});

// handlers triggered by inputView events
Expand Down

0 comments on commit 04d8f2a

Please sign in to comment.