1

I have a problem refactoring a keyup event that I have in my system, the event initially looks like this:

$('#quotation_search_client').on('keyup',function(){
    param=$('#quotation_search_client').val();
    if(param.length>=4){
        var params = {
            social_reason: $('#quotation_search_client').val()
        }
        var clients=ClientServices.getByFilter(params);
        var addresses=ClientServices.getAddresses(clients.clients);
        QuotationServices.fillClients(clients.clients, addresses);      
    }else{
        $('#customers_results').html("");
    }
});

When it's like that, it works perfectly, but when I change it to this:

$('#quotation_search_client').on('keyup',QuotationServices.searchClient({social_reason: $('#quotation_search_client').val()}));

And define the function "searchClient" like this:

    QuotationServices.searchClient = function(params){
        param=$('#quotation_search_client').val();
        if(param.length>=4){
            var clients=ClientServices.getByFilter(params);
            var addresses=ClientServices.getAddresses(clients.clients);
            QuotationServices.fillClients(clients.clients, addresses);      
        }else{
            $('#customers_results').html("");
        }
    };

It stops working, nothing happens when I type something. The console doesn't display any errors so I wrote a console.log at the beginning of the searchClient function and apparently it fires when my page loads, showing that the params.social_reason comes with an empty string, but when I type something, as I said, nothing happens. I don't know what I'm missing, all I did was copy/paste the event's code into the searchClient function and delete the var params (because I'll receive it as a parameter), any help?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Dukra
  • 37
  • 4
  • 1
    same answer different question. I considered using http://stackoverflow.com/q/8462381/497418 or any of the myriad of other related questions, but they're mostly `setTimeout` related. In the end, the same basic problem. – zzzzBov Apr 08 '16 at 20:17

1 Answers1

1

You are executing the function right after the declaration of the keyup event, so you are not passing the function to keyup event.

You should instead do:

$('#quotation_search_client').on('keyup',QuotationServices.searchClient);

And change:

QuotationServices.searchClient = function(){
    var params = {social_reason: $('#quotation_search_client').val()}
    var param = $('#quotation_search_client').val();
    if(param.length>=4){
        var clients=ClientServices.getByFilter(params);
        var addresses=ClientServices.getAddresses(clients.clients);
        QuotationServices.fillClients(clients.clients, addresses);      
    }else{
        $('#customers_results').html("");
    }
};
Pablo Matias Gomez
  • 5,612
  • 6
  • 34
  • 69
  • Interesting, the problem is that I don't want the params to be in the code block because I want to add that same functionality to another element but with a different params value, but this indeed answered my question so thank you! I'll have to do it in another way I guess. – Dukra Apr 08 '16 at 20:23