Proxy_wev8.js
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Ext JS Library 0.20
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.sql.Proxy = function(conn, table, keyName, store, readonly){
Ext.sql.Proxy.superclass.constructor.call(this);
this.conn = conn;
this.table = this.conn.getTable(table, keyName);
this.store = store;
if (readonly !== true) {
this.store.on('add', this.onAdd, this);
this.store.on('update', this.onUpdate, this);
this.store.on('remove', this.onRemove, this);
}
};
Ext.sql.Proxy.DATE_FORMAT = 'Y-m-d H:i:s';
Ext.extend(Ext.sql.Proxy, Ext.data.DataProxy, {
load : function(params, reader, callback, scope, arg){
if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
this.conn.on('open', function(){
this.load(params, reader, callback, scope, arg);
}, this, {single:true});
return;
};
if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
var clause = params.where || '';
var args = params.args || [];
var group = params.groupBy;
var sort = params.sort;
var dir = params.dir;
if(group || sort){
clause += ' ORDER BY ';
if(group && group != sort){
clause += group + ' ASC, ';
}
clause += sort + ' ' + (dir || 'ASC');
}
var rs = this.table.selectBy(clause, args);
this.onLoad({callback:callback, scope:scope, arg:arg, reader: reader}, rs);
}else{
callback.call(scope||this, null, arg, false);
}
},
onLoad : function(trans, rs, e, stmt){
if(rs === false){
this.fireEvent("loadexception", this, null, trans.arg, e);
trans.callback.call(trans.scope||window, null, trans.arg, false);
return;
}
var result = trans.reader.readRecords(rs);
this.fireEvent("load", this, rs, trans.arg);
trans.callback.call(trans.scope||window, result, trans.arg, true);
},
processData : function(o){
var fs = this.store.fields;
var r = {};
for(var key in o){
var f = fs.key(key), v = o[key];
if(f){
if(f.type == 'date'){
r[key] = v ? v.format(Ext.sql.Proxy.DATE_FORMAT,10) : '';
}else if(f.type == 'boolean'){
r[key] = v ? 1 : 0;
}else{
r[key] = v;
}
}
}
return r;
},
onUpdate : function(ds, record){
var changes = record.getChanges();
var kn = this.table.keyName;
this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
record.commit(true);
},
onAdd : function(ds, records, index){
for(var i = 0, len = records.length; i < len; i++){
this.table.insert(this.processData(records[i].data));
}
},
onRemove : function(ds, record, index){
var kn = this.table.keyName;
this.table.removeBy(kn + ' = ?', [record.data[kn]]);
}
});