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

typeahead: add "selected" custom event #3102

Closed
evil-shrike opened this issue Apr 16, 2012 · 10 comments
Closed

typeahead: add "selected" custom event #3102

evil-shrike opened this issue Apr 16, 2012 · 10 comments
Labels

Comments

@evil-shrike
Copy link

I need to handle a case when a user selects an options from dropdown menu. I need to distinguish entered text and chosen from dropdown menu. It's imposible without the custom event.

, select: function () {
  var val = this.$menu.find('.active').attr('data-value')
  this.$element.val(val)
  this.$element.change();
  this.$element.trigger("selected");    // <- ADD THIS
  return this.hide()
}
@fat
Copy link
Member

fat commented Apr 18, 2012

change is an event :)

@fat fat closed this as completed Apr 18, 2012
@evil-shrike
Copy link
Author

No. "change" is an event when something has changed in input box. But I need to distinguish cases when something is being entered in input and was chosen from dropdown menu.
Here's a real case:
Given a control for choosing a country. After entering two symbols a menu with country list is shown. But an application need to react not on entering some symbols in an input (i.e. on "change" event) but on a choice of some country from the list (i.e. on "selected" event).

p.s. please let me know if I couldn't describe my idea well

@fat
Copy link
Member

fat commented Apr 18, 2012

no - change is being manually triggered: this.$element.change()

@evil-shrike
Copy link
Author

right - change is being triggered in select method manually but is also triggered by input itself. And in my callback subscribed on the event I can't see difference between "change" from "select" method and "change" from DOM/jQuery.

@fat
Copy link
Member

fat commented Apr 18, 2012

i think you should listen to blur for your case above

@evil-shrike
Copy link
Author

How can blur help? In blur-handler I can get value from input but I can't be sure that it's was chosen from the list.
I'm trying to say that "autocomplete input" can be used in different cases:

  • input is master and autocomplete menu is just a helper
  • input is just a helper for choosing from master menu

In the latter case we need a separated event for selecting a value from the list.

@fat
Copy link
Member

fat commented Apr 18, 2012

Blur could help - if you're waiting for a user to complete what they intend on inputing. But if you're really set on adding this event i'm open to it.

You'll want to reopen a pull request against 2.0.3-wip though - and add preventDefault support (like this: https://github.com/twitter/bootstrap/blob/2.0.3-wip/js/bootstrap-modal.js#L45)

You'll also need a test and docs. thanks!

@evil-shrike
Copy link
Author

Here's the pull request: #3158 (tests and docs are included).
I didn't build docs, only changed mustache template, is it right?

@finom
Copy link

finom commented Sep 23, 2012

To make custom events for typeahead you can use my thing: https://github.com/finom/Functions/tree/master/FunctionCallEvent#why-is-it-needed

@grantzau
Copy link
Contributor

The "selected" event would be useful in some cases. It often makes a difference, wether the user did select input from the typeahead list, or manually wrote some random input. But the "change" even fires in both cases.

A simple/hack way to tell the difference using the "change" event would be:

$(".typeahead").on("change", function (e) {

    if (!e.originalEvent) {
        // the change event was manually triggered
    }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants