Custom event suggestions
I was pretty conservative with what custom events I added in v0.9.0, now it's time to get a little crazier. Below is a combination of events I think would be nice to have and some events I've seen people asking for:
- autocompleted – when the user
tabsto select the suggestion the hint is showing. - rendered – suggestions for a given query were retrieved and rendered.
- prefetch failed – requesting or processing the prefetched data fails for whatever reason.
- nothing left – no suggestions were found for a given query.
- suggestion highlighted – the user mouses or keys over a suggestion in the dropdown menu.
- changed – emulate the native change event of an input element.
I'm not sure implementing all of these is plausible, but I at least wanted to document them so a discussion could be had.
Any other custom events you'd like to see implemented?
In regards to the autocompleted event, I could see an argument being made that it should be lumped into typehead:selected
Eh scratch that, typeahead:autocompleted should be separate from typeahead:selected.
First of all: awesome work! Keep it up! I would be happy about an event being triggered as soon as a remote request is being fired and another one when data comes back from the remote source.
@FLOKER just so I understand the use case, how would you use those events?
I know that my remote source takes about 4 - 10 seconds to answer the request typeahead.js sends. And I would like to show something like a spinning wheel or any kind of indicator.
For example:
$('.typeahead').typeahead({
...
}).on('typeahead:requestSentOff', function() {
showSpinningWheel();
}).on('typeahead:remoteDataReceived', function() {
hideSpinningWheel();
});
I don't really know if typeahead:remoteDataReceived is actually the same like typeahead:opened, but I noticed that typeahead:opened gets already fired when I focus my input field, even without having entered something...
Thanks in advance!
Edit: I just noticed the perfect example of what I would like to do is the GitHub issue-search in projects. Just if you go to Issues and type something in the upper right hand searchbox, you notice the small indicator on the left. :grin:
Should the typeahead:opened fire on focus if the actual .tt-dropdown-menu is not displayed?
@joelataylor first some context. The dropdown menu has 2 properties that determine whether or not it is visible: isOpened and isEmpty. The only time the dropdown menu is visible is isOpened === true && isEmpty === false. typeahead:opened gets triggered when isOpened toggles from false to true.
The events that can toggle isOpened to true:
- input gains focus
- input has focus and the query changes
- input has focus and either the up or down arrow are pressed
The events that can toggle isOpened to false:
- input loses focus
- the esc key is pressed
With this being the case, I think the way typeahead:opened is currently triggered is fine. What needs to be added is some sort of event that is triggered when suggestions become visible. I'm guessing the typeahead:rendered event I mentioned previously in this thread will fill that void.
@FLOKER sorry I thought I replied to your last comment. I like the idea of being able to replicate how the GitHub issue search works. I'll try and think of a good way to achieve that functionality.
@jharding thanks. I think something like the previously mentioned typeahead:rendered would work to hide for example the spinning wheel indicator. You would only have to think about an event being fired when the query to the remote server is being made.
$('.typeahead').typeahead({
...
}).on('typeahead:remoteRequestSentOff', function() {
// gets fired when typeahead makes a query to the remote source
showSpinningWheel();
}).on('typeahead:rendered', function() {
hideSpinningWheel();
});
@jharding thanks for the explanation. So if I could check the isEmpty property I could properly toggle - although the opened event would only fire once. I'd have to setup a keyup listener. Hmmm ... well I look forward to the new events!! :)
I could really use the custom typeahead:suggestionhighlighted. I want to recreate part of Yahoo's 'Search Assist' search box, but right now it's only possible to capture the arrow key events to find out which result is highlighted:
http://www.visualmedia.nl/html5/supersearch/
How are you going about capturing the key events?
Alright, I see you're listening for keydown. Is there a reason why you can't do something similar with mouseenter?
There must be a reason, because this doesn't trigger the handler:
$('.typeahead').on('mouseover', '.tt-suggestion', function(e){
console.log('fire');
setPreview();
});
I'm looking at the source of typeahead.js right now to figure out where it goes wrong, but jQuery and events are not my friends..
Try:
$('.twitter-typeahead').on('mouseover', '.tt-suggestion', function(e){
console.log('fire');
setPreview();
});
Thanks Mr. Harding, you pushed me in the right direction. It was actually:
$('.tt-dropdown-menu').on('mouseover', '.tt-suggestion', function(e){
setPreview();
});
The mockup now works like a charm!
Hi
Is there a way of obtaining the Dataset name of the selected datum from custom events 'typeahead:selected' or 'typeahead:autocompleted'? I want to have multiple datasets for a single input and have callback functionality specific to each dataset.
Thanks Ste
@steswinbank the only way you can do that right now is by adding additional information to the datums.
$('.typeahead').typeahead({
name: 'dataset1',
local: [
{ value: 'val1', dataset: 'dataset1' },
{ value: 'val2', dataset: 'dataset1' }
]
});
I need what @steswinbank said too, and I really don't want to waste bandwidth for an attribute that is already known to typeahead.
I've found a way of obtaining the dataset, but this is a pretty fragile hack relying on the markup not changing structure:
o.getDatasetName = function (inputEl) {
return $(inputEl).parent().find("div.tt-suggestion").parent().parent().attr("class").substr(11)
}
This returns a string with the dataset name.