2012-07-16 82 views
1

我看下面的例子scottgonzalez/jquery-ui-extensions自動完成:如何將額外的參數傳遞給響應回調

我需要定製source callback,這需要兩個argumnets requestresponse爲自動完成。

我的問題如下: 如何將一個額外的參數傳遞給源回調以便根據自動完成中定義的變量過濾數據? 示例:

currentUser = false - >我需要從源中排除currentUser。

這是我的代碼(1)(2)。
請參閱評論以更好地瞭解我所問的內容。
謝謝。


(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; 
}); 

回答

2

你可以只是包裝 「匹配」 的電話到功能:

source: function(request, response) { 
    return matcher(request, response, {isCurrentUser : true}); 
} 
+1

+1沒有在插件代碼中處理這種情況的方式,因爲這意味着匹配器的實現細節會泄漏到自動完成中。所以需要在兩個插件連線時完成 - >用戶代碼。 – 2012-07-16 09:42:31

相關問題