3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">Ext.sql.Proxy = function(conn, table, keyName, store, readonly){
\r
9 Ext.sql.Proxy.superclass.constructor.call(this);
\r
11 this.table = this.conn.getTable(table, keyName);
\r
14 if (readonly !== true) {
\r
15 this.store.on('add', this.onAdd, this);
\r
16 this.store.on('update', this.onUpdate, this);
\r
17 this.store.on('remove', this.onRemove, this);
\r
21 Ext.sql.Proxy.DATE_FORMAT = 'Y-m-d H:i:s';
\r
23 Ext.extend(Ext.sql.Proxy, Ext.data.DataProxy, {
\r
24 load : function(params, reader, callback, scope, arg){
\r
25 if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
\r
26 this.conn.on('open', function(){
\r
27 this.load(params, reader, callback, scope, arg);
\r
28 }, this, {single:true});
\r
31 if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
\r
32 var clause = params.where || '';
\r
33 var args = params.args || [];
\r
34 var group = params.groupBy;
\r
35 var sort = params.sort;
\r
36 var dir = params.dir;
\r
39 clause += ' ORDER BY ';
\r
40 if(group && group != sort){
\r
41 clause += group + ' ASC, ';
\r
43 clause += sort + ' ' + (dir || 'ASC');
\r
46 var rs = this.table.selectBy(clause, args);
\r
47 this.onLoad({callback:callback, scope:scope, arg:arg, reader: reader}, rs);
\r
49 callback.call(scope||this, null, arg, false);
\r
53 onLoad : function(trans, rs, e, stmt){
\r
55 this.fireEvent("loadexception", this, null, trans.arg, e);
\r
56 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
59 var result = trans.reader.readRecords(rs);
\r
60 this.fireEvent("load", this, rs, trans.arg);
\r
61 trans.callback.call(trans.scope||window, result, trans.arg, true);
\r
64 processData : function(o){
\r
65 var fs = this.store.fields;
\r
68 var f = fs.key(key), v = o[key];
\r
70 if(f.type == 'date'){
\r
71 r[key] = v ? v.format(Ext.sql.Proxy.DATE_FORMAT,10) : '';
\r
72 }else if(f.type == 'boolean'){
\r
82 onUpdate : function(ds, record){
\r
83 var changes = record.getChanges();
\r
84 var kn = this.table.keyName;
\r
85 this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
\r
86 record.commit(true);
\r
89 onAdd : function(ds, records, index){
\r
90 for(var i = 0, len = records.length; i < len; i++){
\r
91 this.table.insert(this.processData(records[i].data));
\r
95 onRemove : function(ds, record, index){
\r
96 var kn = this.table.keyName;
\r
97 this.table.removeBy(kn + ' = ?', [record.data[kn]]);
\r