03
06
2010
Ext.ux.data.PagingMemoryProxyの改良版 for Ext JS 3.x
Posted by: Moriwaki in Ext JS, JavaScript元はページを切り替えるたびにRecordを作り直していたので、これを修正。
if (!Array.prototype.map) {
Array.prototype.map = function(fun) {
var len = this.length;
if (typeof fun != 'function') {
throw new TypeError();
}
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
res[i] = fun.call(thisp, this[i], i, this);
}
}
return res;
};
}
Ext.ns('Ext.ux.data');
Ext.ux.data.PagingMemoryProxy = Ext.extend(Ext.data.MemoryProxy, {
customFilter : null,
resultMemory : null,
constructor : function(data) {
Ext.ux.data.PagingMemoryProxy.superclass.constructor.call(this);
this.data = data;
},
insert : function(record) {
this.resultMemory.records.unshift(record);
this.resultMemory.totalRecords = this.resultMemory.records.length;
},
remove : function(record) {
for (i = this.resultMemory.records.length - 1; i >= 0; i--) {
if (this.resultMemory.records[i].id == record.id) {
this.resultMemory.records.splice(i, 1);
this.resultMemory.totalRecords = this.resultMemory.records.length;
return;
}
}
},
doRequest : function(action, rs, params, reader, callback, scope, options) {
params = params || {};
var result = {
success : null,
records : null,
totalRecords : null
};
try {
if (!this.resultMemory) {
this.resultMemory = reader.readRecords(this.data);
}
} catch (e) {
this.fireEvent('loadexception', this, options, null, e);
callback.call(scope, null, options, false);
return;
}
// filtering
if (params.filter !== undefined) {
this.resultMemory.records = this.resultMemory.records.filter(function(el) {
if (typeof(el) == 'object') {
var att = params.filterCol || 0;
return String(el.data[att]).match(params.filter)
? true
: false;
} else {
return String(el).match(params.filter)
? true
: false;
}
});
this.resultMemory.totalRecords = this.resultMemory.records.length;
}
// sorting
if (params.sort !== undefined) {
var dir = String(params.dir).toUpperCase() == 'DESC' ? -1 : 1;
var fn = function(v1, v2) {
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
};
this.resultMemory.records.sort(function(a, b) {
var v = 0;
if (typeof(a) == 'object') {
v = fn(a.data[params.sort], b.data[params.sort])
* dir;
} else {
v = fn(a, b) * dir;
}
if (v == 0) {
v = (a.index < b.index ? -1 : 1);
}
return v;
});
}
// paging (use undefined cause start can also be 0 (thus false))
if (params.start !== undefined && params.limit !== undefined) {
result.records = [];
var index = 0;
for (var i = params.start; i < params.start + params.limit; i++) {
if (this.resultMemory.records[i]) {
result.records[index++] = this.resultMemory.records[i];
} else {
break;
}
}
}
result.success = this.resultMemory.success;
result.totalRecords = this.resultMemory.totalRecords;
callback.call(scope, result, options, true);
}
});
// backwards compat.
Ext.data.PagingMemoryProxy = Ext.ux.data.PagingMemoryProxy;
Trackback URL:
