Ext2的一些扩展

本文转贴

extjsHacker.js
=============

//Extjs 2.X
//为grid ColumnModel 的renderer增加gridView的引用
//
Ext.grid.GridView.prototype.doRender = function(cs, rs, ds, startRow, colCount, stripe){
        var ts = this.templates, ct = ts.cell, rt = ts.row, last = colCount-1;
        var tstyle = 'width:'+this.getTotalWidth()+';';
        // buffers
        var buf = [], cb, c, p = {}, rp = {tstyle: tstyle}, r;
        for(var j = 0, len = rs.length; j < len; j++){
            r = rs[j]; cb = [];
            var rowIndex = (j+startRow);
            for(var i = 0; i < colCount; i++){
                c = cs[i];
                p.id = c.id;
                p.css = i == 0 ? 'x-grid3-cell-first ' : (i == last ? 'x-grid3-cell-last ' : '');
                p.attr = p.cellAttr = "";
                p.value = c.renderer(r.data[c.name], p, r, rowIndex, i, ds,this);
                p.style = c.style;
                if(p.value == undefined || p.value === "") p.value = "&#160;";
                if(r.dirty && typeof r.modified[c.name] !== 'undefined'){
                    p.css += ' x-grid3-dirty-cell';
                }
                cb[cb.length] = ct.apply(p);
            }
            var alt = [];
            if(stripe && ((rowIndex+1) % 2 == 0)){
                alt[0] = "x-grid3-row-alt";
            }
            if(r.dirty){
                alt[1] = " x-grid3-dirty-row";
            }
            rp.cols = colCount;
            if(this.getRowClass){
                alt[2] = this.getRowClass(r, rowIndex, rp, ds);
            }
            rp.alt = alt.join(" ");
            rp.cells = cb.join("");
            buf[buf.length] =  rt.apply(rp);
        }
        return buf.join("");
    }

//extjs 2.x 
/**
 * 为combobox添加lookup
 * @param {} v
 * @param {} rec
 */
Ext.form.ComboBox.prototype.setValue = function(v,rec){
       var text = v;

        if(this.valueField){
            var r = this.findRecord(this.valueField, v);

            if(r){
                text = r.data[this.displayField];
            }else if(this.valueNotFoundText !== undefined){
                text = this.valueNotFoundText;
            }
        }
        //添加lookup
        if (rec && this.record && text) {  
              if(this.lookup){
                  for(var i = 0 ; i<this.lookup.length ; i++){
                      this.record.set(this.lookup[i][0],rec.get(this.lookup[i][1]));
                  }
              }
         }
         
        this.lastSelectionText = text;
        if(this.hiddenField){
            this.hiddenField.value = v;
        }
        Ext.form.ComboBox.superclass.setValue.call(this, text);
        this.value = v;
    };
    
//extjs 2.x
/**
 * 为gridEditor添加行数据引用
 * @param {} el
 * @param {} value
 */
Ext.grid.GridEditor.prototype.startEdit = function(el, value){  
         if(this.editing){  
             this.completeEdit();  
         }  
         this.boundEl = Ext.get(el);  
         var v = value !== undefined ? value : this.boundEl.dom.innerHTML;  
         if(!this.rendered){  
             this.render(this.parentEl || document.body);  
         }  
         if(this.fireEvent("beforestartedit", this, this.boundEl, v) === false){  
             return;  
         }  
         //alert(this.field);
         this.startValue = v;  
         this.field.record = this.record;//就加入这一行,让field中获得当前数据行  
         this.field.setValue(v);  
         this.doAutoSize();  
         this.el.alignTo(this.boundEl, this.alignment);  
         this.editing = true;  
         this.show();  
     } 
    
//extjs 2.x  Ext.lib.Ajax
/**
 * 添加同步ajax
 * @param {} method
 * @param {} uri
 * @param {} callback
 * @param {} postData
 * @return {}
 */     
Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData) {
    
            var o = this.getConnectionObject();

            if (!o) {
                return null;
            }
            else {
                o.conn.open(method, uri, false);

                if (this.useDefaultXhrHeader) {
                    if (!this.defaultHeaders['X-Requested-With']) {
                        this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
                    }
                }

                if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){
                    this.initHeader('Content-Type', this.defaultPostHeader);
                }

                 if (this.hasDefaultHeaders || this.hasHeaders) {
                    this.setHeader(o);
                }

                this.handleReadyState(o, callback);
                o.conn.send(postData || null);

                return o;
            }
        };
        
//extjs 2.x  Ext.lib.Ajax  
/**
 * 添加同步ajax
 * @param {} method
 * @param {} uri
 * @param {} cb
 * @param {} data
 * @param {} options
 * @return {}
 */        
Ext.lib.Ajax.request = function(method, uri, cb, data, options) {
            if(options){
                var hs = options.headers;
                if(hs){
                    for(var h in hs){
                        if(hs.hasOwnProperty(h)){
                            this.initHeader(h, hs[h], false);
                        }
                    }
                }
                if(options.xmlData){
                    if (!hs || !hs['Content-Type']){
                        this.initHeader('Content-Type', 'text/xml', false);
                    }
                    method = (method ? method : (options.method ? options.method : 'POST'));
                    data = options.xmlData;
                }else if(options.jsonData){
                    if (!hs || !hs['Content-Type']){
                        this.initHeader('Content-Type', 'application/json', false);
                    }
                    method = (method ? method : (options.method ? options.method : 'POST'));
                    data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
                }
            }
            if ("sync" in options) return this.syncRequest(method, uri, cb, data);
            return this.asyncRequest(method, uri, cb, data);
        };   

//extjs 2.x 自动重连
/**
 * Ext.Ajax.request({
   url: 'foo.jsp',
   timeout:2,
   
   //重连次数
   retryCount:3,
   
   //重连后要注意保存新的 newTransactionId
   retry:function(retryCount,newTransactionId){
       
       alert('failture remain retry count :' + retryCount+'\n'
       +'newTransactionId : '+newTransactionId.tId);
   },
   
   //最终的失败函数
   failure: function(responseObject){
      alert('failture finally :'+responseObject.statusText);
   },
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});
*/
(function(){
    //保存原来的调用代码
    var oldRequest=Ext.lib.Ajax.request;
    //新的增强代码
    Ext.lib.Ajax.request = function(method, uri, cb, data, options) {
        //没有配置就运行老代码
        if(!options.retryCount) 
            return oldRequest.call(Ext.lib.Ajax,method, uri, cb, data, options);
        
        //保存原来的失败处理函数
        var oldFailure=cb.failure;
        //添加了自动重连的新的处理函数
        cb.failure=function(responseObject){
            options.retryCount--;
            if(options.retryCount) {
                //注意:自动重连后tId 变化
                var newTransactionId = oldRequest.call(Ext.lib.Ajax,method, uri, cb,data, options);
                //调用重连回调函数
                if(options.retry) {
                    options.retry.apply(cb.scope||window, [options.retryCount,newTransactionId]);
                }
            }    
            //重连够了,调用真正的失败函数。
            else if(oldFailure){
                oldFailure.apply(cb.scope||window, [responseObject]);
            }
        }
        
        //增强配置,再运行老代码
        return oldRequest.call(Ext.lib.Ajax,method, uri, cb, data, options);
                
    }
    
})();        
        
//extjs 2.x
/**
 * 为form添加当前数据record
 * @param {} values
 * @return {}
 */
Ext.form.BasicForm.prototype.setValues = function(values){
        this.data = values; //添加数据引用,用于以后使用
        if(Ext.isArray(values)){ // array of objects
            for(var i = 0, len = values.length; i < len; i++){
                var v = values[i];
                var f = this.findField(v.id);
                if(f){
                    f.setValue(v.value);
                    if(this.trackResetOnLoad){
                        f.originalValue = f.getValue();
                    }
                }
            }
        }else{ // object hash
            var field, id;
            for(id in values){
                if(typeof values[id] != 'function' && (field = this.findField(id))){
                    field.setValue(values[id]);
                    if(this.trackResetOnLoad){
                        field.originalValue = field.getValue();
                    }
                }
            }
        }
        return this;
    }        
    
    
//Extjs 2.x 
Ext.FormPanel.prototype.initFields = function(){
        var f = this.form;
        var formPanel = this;
        var fn = function(c){
            if(c.isFormField){
                c.form = formPanel; //为录入控件添加form的引用
                f.add(c);
            }else if(c.doLayout && c != formPanel){
                Ext.applyIf(c, {
                    labelAlign: c.ownerCt.labelAlign,
                    labelWidth: c.ownerCt.labelWidth,
                    itemCls: c.ownerCt.itemCls
                });
                if(c.items){
                    c.items.each(fn);
                }
            }
        }
        this.items.each(fn);
    }

//Extjs 2.x
Ext.FormPanel.prototype.beforeDestroy = function(){
        var fn=function(c){
            if (c.isFormField) {
                if(c.form) {
                    c.form=null;
                    delete c.form;
                }
            }
            if(c.items){
                c.items.each(fn);
            }
        };
        this.items.each(fn);
        Ext.FormPanel.superclass.beforeDestroy.call(this);
        this.stopMonitoring();
        Ext.destroy(this.form);
    }    
    
Ext.override(Ext.form.BasicForm, {
  setReadOnly: function(bReadOnly){
    this.items.each(function(f)
    {
      if (f.isFormField)
      {
        f.getEl().dom.readOnly = bReadOnly;                
        // Remove click event handlers
        if (f instanceof Ext.form.TriggerField)        
        {
          if (bReadOnly)
            f.trigger.un('click', f.onTriggerClick, f)
          else
            f.trigger.on('click', f.onTriggerClick, f, {preventDefault:true});
          
          if (f instanceof Ext.form.ComboBox) //Alternatively, to check if combobox use: if (f.setEditable)...
          {
              if(f.view){
                if (bReadOnly){
                    f.view.un('click', f.onViewClick, f)
                }
                else{
                    f.view.on('click', f.onViewClick, f);
                }
              }
          }
        }
      }
    });    
  }
}); 

你可能感兴趣的:(C++,c,Ajax,ext,C#)