-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Trigger custom events #68
Comments
for the suggestion selected event, I'd find it useful if the event supplied the entire datum selected instead of just the value |
That's the plan 😄 |
I could think of:
|
On "nothing left" when the request typed is too specific and there is nothing to display. |
Would it be alright if not all of the events were triggered on the typeahead input element? Datasets are encapsulated away from the DOM, so it wouldn't be trivial to trigger events like |
@r4vi for the use case you described, you'd probably want to rely on remote data and turn caching off. |
@jharding That'd be all right for me, as long as out could find out which typeahead instance triggered the event (in the rare cases where you't have multiple typeahead instances in one document). |
@jharding agree on the use-case I mentioned, was just trying to think of ways people might use it. wouldn't it be better to return an object that you could watch for events on initialization of typeahead?
which, on second thought, does break jQuery chaining, so maybe something like this would be better
|
I was imagining the I'm not a fan of returning an object on typeahead initialization because it breaks jQuery chainability. |
we should consider the implication of datasets with the same name being used in more than one typeahead; what if you have two typeaheads using a common dataset? e.g.
Would this give us the following output? This also raises the question of deduplicatng the dataset persistent cache, should we hit I'd say for 0.9 go for events on the document for each dataset and see what feedback we get from people doing weird stuff. |
I could imagine a use case for exposing a "selectionHighlighted" event. This would allow other portions of the application to know when a selection is highlighted (via mouse hover or keyboard navigation). By knowing when a selection is highlighted, you can make other portions of the UI respond (e.g. updating an image preview based on the highlighted selection) all without typeahead's knowledge. I don't know what this event would be called, but it would clearly happen before the 'selected' event. As far as the means for event communication/propagation, I could see something like r4vi's idea of passing in an event dispatcher as an argument to typeahead. This would allow the developer to have flexibility with regards to event management. And example with backbone would look like the following, for example: var MyEventDispatcher = _.clone(Backbone.Events);
/* ...application code... */
$('input').typeahead({
dispatcher: MyEventDispatcher
});
MyEventDispatcher.on('typeaheadSelectionHighlighted', updateSelectionPreview); I used the Backbone.Events object here, but it leaves it to the developer to decide what kind of object to use. |
👍 I would also see use cases of this. See http://api.jqueryui.com/autocomplete/ It would be awesome to have at least those:
I would differentiate between 'change' and 'close' because a user might close the suggestions by clicking somewhere else without actually changing the selection. What do you think? |
Nothing is going to ship today, the hope is to have v0.9.0 out next Monday. |
@r4vi datasets are identified by name, so in your example code, only one var datasetCache = {};
function initialize(datasetDef) {
var dataset;
if (datasetCache[datasetDef]) {
dataset = datasetCache[datasetDef];
}
else {
dataset = new Dataset(datasetDef);
datasetCache[datasetDef.name] = dataset;
}
} In the next revision of the README, I'm hoping to make it clear that this is what's going on. |
Just by overwritting _handleSelection I could make everything work here.
Works like the 'engine' option. |
@jharding damn, hope to actually learn js someday...nice code. |
@nofxx would something like this work: $('.typeahead').typeahead({ /* ... */ })
.on('typeahead:selected', function($e) {
var $typeahead = $(this),
$form = $typeahead.parents('form').first();
$form.submit();
}); |
I'm doing exactly that with my current autocomplete plugin |
@jharding thanks for the code , it's pasted here. I'll try with 0.9, my overwrited version is working til there. |
For those of you who are interested, here's a list of events that will be triggered in v0.9.0:
All of these events will be triggered on the typeahead input. |
@jharding Which does "selected" mean:
On Wed, Mar 13, 2013 at 1:23 AM, Jake Harding [email protected]:
Devon Ray Williams :: Devon Ray Music http://devonraymusic.com/ :: How to |
@devonray I believe it's (2) |
2 On Wednesday, March 13, 2013, Ravi Kotecha wrote:
|
Mr Harding... is the event code now in 0.9.0 branch? I can't see it... |
Not yet, it'll be in there sometime tonight. |
@rayui the event code is now available in |
I've been playing around with typeahead, and another event that might make sense is if some text is typed in, but no match is found. |
FYI, all future discussion concerning custom events should happen over at #130. |
Hey, @r4vi , @jharding , @ankane <% user.avatar %>
<% user.followers.count %> I want to develop autocomplete searching with displaying user's avatar like above! |
You need to place them in the objects that are passed to typeahead (or Bloodhound if you're using that). |
There's a handful of issues asking for something along the lines of a "selection callback". While I agree this is necessary, I don't think adding an option like
onSelection
is the best way to go about it. Instead, I think triggering custom events on the typeahead input is the way to go. It's a more extensible approach and it won't pollute the options hash.I'm creating this ticket to start brainstorming what events we need to trigger. The only 2 I can think of right now are:
If there's anything else you'd like an event triggered for, leave a comment in this issue.
The text was updated successfully, but these errors were encountered: