使用CachedAutocompleter提高Autocompleter性能

使用过scriptculous的人可能都会用到它的Autocompleter组件,确实很Cool的东东,但是相对于用户的输入速度来说,频繁的Ajax请求有时会滞后很多,使用cache是一种较好的解决方案,虽然它不能解决全部问题,但是如果一个页面有很多地方需要这种Autocompleter带来用户体验的提升还是很明显的。^_^
下面给出CachedAutocompleter的一种实现:
Ajax.CachedAutocompleter = Class.create();
Object.extend(Object.extend(Ajax.CachedAutocompleter.prototype, Autocompleter.Base.prototype), {
initialize: function(element, update, url, options, externalToken) {
this.baseInitialize(element, update, options);
this.options.asynchronous = true;
this.options.onComplete = this.onComplete.bind(this);
this.options.defaultParams = this.options.parameters || null;
this.url = url;
this.cache = {};
this.cacheKey = "";
},

getUpdatedChoices: function() {
this.cacheKey = this.getToken();
entry = encodeURIComponent(this.options.paramName) + '=' +
encodeURIComponent(this.cacheKey);
this.options.parameters = this.options.callback ?
this.options.callback(this.element, entry) : entry;
if(this.options.defaultParams)
this.options.parameters += '&' + this.options.defaultParams;
if(this.options.parameters)
this.cacheKey = this.options.parameters;
if(this.cache[this.cacheKey]) {
this.updateChoices(this.cache[this.cacheKey]);
} else {
new Ajax.Request(this.url, this.options);
}
},
onComplete: function(request) {
this.updateChoices(this.cache[this.cacheKey] = request.responseText);
}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值