antennaio / jquery-bar-rating

jQuery Bar Rating Plugin - minimal, light-weight jQuery ratings.
http://antennaio.github.io/jquery-bar-rating/
MIT License
736 stars 263 forks source link

.change() called while silent is true #82

Closed bbreukelen closed 7 years ago

bbreukelen commented 7 years ago

When using the $("select").on("change" event to listen when the score has changed, this event is also triggered when I do $("select").barrating('set', 5); which wouldn't be expected when the silent option is set to true.

Can you please change the code in setSelectFieldValue to only execute self.$elem.change() when silent is true?

Thanks for the great plugin by the way!

antennaio commented 7 years ago

One question @bbreukelen, why are you using 'change' event when you could be using 'onSelect' callback, that was specifically created for this purpose?

bbreukelen commented 7 years ago

I have a form with many input/select fields. This is generated in a library which handles the dynamic creation of form-fields and listening to their value-changes. I don't want to make exceptions to the existing code for each filter but instead keep the form-field handling in-place as much as possible. This is the reason why I'm not using the onSelect callback.

If you ask me, the brilliance of your plugin is that with a single line of code, an existing select field that can be used as-is, is converted into something beautiful.

antennaio commented 7 years ago

Ok, I think I get it - you'd prefer to use the 'change' event.

I'm sort of in a difficult situation here, because other people may rely on the 'change' event or framework like Knockout could rely on it to recognise that a select field has a new selected value - here's a link to a pull request that added the .change() call.

https://github.com/antennaio/jquery-bar-rating/pull/25

By removing it now it may fix the code for you, but break it for other people.

bbreukelen commented 7 years ago

Hmm, I was wondering why the change event was being called in the first place, but that explains it.

How about another option with a default setting to work for knockout? Something like: triggerChange: (true by default) It will always trigger the change event when manually clicking on the bar. It will only suppress it if triggerChange is false and the value is programmatically set.

antennaio commented 7 years ago

Hi @bbreukelen

I added a 'triggerChange' option that will disable change() calls on the select field. To make the plugin behave as you want you'd probably need to use the following combination of options:

$('#example-movie').barrating('show', {
  theme: 'your-theme',
  silent: true,
  triggerChange: false,
  onSelect: function() {
    $('#example-movie').change(); // manually trigger change event when a rating is selected
  }
});