PUGX / PUGXAutoCompleterBundle

Add an autocomplete field to your Symfony forms
GNU Lesser General Public License v3.0
93 stars 38 forks source link

Add support for a callback function on select (jQueryUI version) #27

Closed Restless-ET closed 8 years ago

Restless-ET commented 8 years ago

I don't think there's much need for something like this on the Select2 version because in there I guess we can make use of the on('change') event binded to the fake input (have not tested that though).

However in the jQueryUI version I was having some troubles executing a custom function when I selected a value for the input, that's why I've made this change.

Thanking @rntdrts regarding this.

garak commented 8 years ago

I'm not sure to understand how you can use your added parameter. Wouldn't be easier to add it like an option?

Restless-ET commented 8 years ago

The idea is to be able to do things like this:

$('#my_input').autocompleter({
    url_list: '/get_results_list',
    min_length: 3
}, function(element) {
    var myUrl = '/my/path' + element.val();
    jQuery.ajax({
        type: 'GET',
        dataType: 'html',
        url: myUrl,
        success: function(data) {
            jQuery('#custom_div').html(data)
        }
    });
});

So that the function (an ajax request in this case) is called/executed when an element provided by the autocompleter is selected.

This could be passed as an option as you suggested but IMO that can be confusing because for me options are intended to serve another purpose (more configuration/settings related).

garak commented 8 years ago

Can you provide an example of library in which options are exclusively used as configuration/settings? As far as I can see, callbacks are pretty ubiquitous in jquery plugins, and that is not surprising, being javascript a functional language

rntdrts commented 8 years ago

@garak you can do that, but i don't think it will make your js code clean and most of the libraries do that, because it's easy when you have multiple events, and yet for me even on that case that's not correct when you can use trigger... But in your case it's only one callback.

And you can still call the autocompleter plugin without a callback method... so? do you still want that as an option?

garak commented 8 years ago

As many other option, the callback can be optional. If no callback is provided, the callback is not called. Super simple

Restless-ET commented 8 years ago

The current implementation still makes the callback optional. I mean you can still use the autocompleter as:

$('#my_input').autocompleter({
    url_list: '/get_results_list',
    min_length: 3
});

No callback needed. :)

As you describe it is like considering url_list as optional which is not true or this won't do anything. This is why I actually see the options as configurations instead of optional parameters.

Anyway, I you do prefer the callback passed in the options list that can be easily adapted..

garak commented 8 years ago

Yes, please change it to be a non-mandatory option. Thanks

Restless-ET commented 8 years ago

Closing in favor of #28.