1
我看下面的例子scottgonzalez/jquery-ui-extensions自动完成:如何将额外的参数传递给响应回调
我需要定制source callback
,这需要两个argumnets request
和response
为自动完成。
我的问题如下: 如何将一个额外的参数传递给源回调以便根据自动完成中定义的变量过滤数据? 示例:
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;
});
+1没有在插件代码中处理这种情况的方式,因为这意味着匹配器的实现细节会泄漏到自动完成中。所以需要在两个插件连线时完成 - >用户代码。 – 2012-07-16 09:42:31