1

I was looking the following example scottgonzalez / jquery-ui-extensions

I need to customise the source callback, which expects two argumnets request and response for the autocomplete.

My question is the following: how can I pass an extra parameter to the source callback in order to filter the data according a variable defined in autocomplete? Example:

currentUser = false -> I need to exclude the currentUser from the source.

Here is my code (1) (2).
Please see the comment to understand better what I am asking.
Thanks.


(1)

// autocomplte.js
define([
   'jquery',
   'matcher'
], function ($, matcher) {
    "use strict";
    var autoComplete = function (element, options) {
        console.log(options);  // {isCurrentUser: true}
        element.autocomplete({
            minLength: 3,
            autoFocus: true,
            source: matcher // this is a callback defined 
                            // in matcher.js
        });
        // other codes;
     }
});

(2)

// matcher.js
define([
    'jquery',
    'users',
    'jqueryUi'
], function ($, UserCollection) {
    "use strict";

    var userCollection,
        matcher;

    matcher = function (request, response, options) { // how can I pass 
                                                      // an extra parameter 
                                                      // to this callback?
        console.log(options); // undefined it should be {isCurrentUser: true}
        userCollection = new UserCollection();
        var regExp = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i');
        response(userCollection.filter(function (data) {
            return regExp.test(data.get('first_name'));
        }));
    };

    return matcher;
});
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134

1 Answers1

2

You can just wrap call of "matcher" into function:

source: function(request, response) { 
   return matcher(request, response, {isCurrentUser : true}); 
} 
Anton L
  • 412
  • 4
  • 12
  • 1
    +1 There is no way to handle this case in the plugins' code because that would mean implementation details of matcher would leak into autocomplete. So it needs to be done when the two plugins are wired -> user code. – Aaron Digulla Jul 16 '12 at 09:42