ExtJS精华函数解析(仅个人见解)
One: Ext的最先开始的精华函数要算Ext.namespace,它采取的是命名空间的方式.从而可以避免各种变量啊.对象的冲突问题
Two: Ext的apply采取的是对象继承方式,Ext.apply(o,c,object),既o继承c的所有属性.并且o也拥有object的一切属性
Three Ext的applyIf采取的对象继承方式,这个跟上面的继承有一点不同,Ext.applyIf(o,c)就是只有当o里的对象不存在时才继承.反之就不继承.
Four: Ext的extend,这个也是整个Ext的精华所在,所有的对象继承,扩展都是根据Ext.extend的方法而来.本人写的一个htmlEditor也才是看懂了extend方法才搞清楚如何写扩展.
htmlEditor表情扩展查看地址:http://extjs.com/forum/showthread.php?t=13213 http://extjs.com/learn/Image:Face.jpg.jpg
Ext.extend(sb, sp, overrides)即sb扩展sp的所有对象,overriders即你想增加的对象或者方法.可以覆盖sp的对象也与方法及属性.
这是本人从ext中扣出来的精华部分,大家完全可以根据这个精华自己去写扩展,写框架.
- /*
- *
- * Author:Sloppy
- * Date:2007-09-15
- * Contact:QQ:35734786
- */
- if(typeof(Ext)=="undefined"){
- var Ext={};
- }
- window["undefined"] = window["undefined"];
- Ext.apply = function(o, c, defaults){
- if(defaults){
- Ext.apply(o, defaults);
- }
- if(o && c && typeof c == 'object'){
- for(var p in c){
- o[p] = c[p];
- }
- }
- return o;
- };
- (function(){
- var ua = navigator.userAgent.toLowerCase();
- var isStrict = document.compatMode == "CSS1Compat",
- isSafari = (/webkit|khtml/).test(ua),
- isIE = ua.indexOf("msie") > -1,
- isIE7 = ua.indexOf("msie 7") > -1,
- isGecko = !isSafari && ua.indexOf("gecko") > -1;
- if(isIE && !isIE7){
- try{
- document.execCommand("BackgroundImageCache", false, true);
- }catch(e){}
- }
- Ext.apply(Ext,{
- emptyFn : function(){},
- applyIf : function(o, c){
- if(o && c){
- for(var p in c){
- if(typeof o[p] == "undefined"){ o[p] = c[p]; }
- }
- }
- return o;
- },
- extend : function(){
- var io = function(o){
- for(var m in o){
- this[m] = o[m];
- }
- };
- return function(sb, sp, overrides){
- if(typeof sp == 'object'){
- overrides = sp;
- sp = sb;
- sb = function(){sp.apply(this, arguments);};
- }
- var F = function(){}, sbp, spp = sp.prototype;
- F.prototype = spp;
- sbp = sb.prototype = new F();
- sbp.constructor=sb;
- sb.superclass=spp;
- if(spp.constructor == Object.prototype.constructor){
- spp.constructor=sp;
- }
- sb.override = function(o){
- Ext.override(sb, o);
- };
- sbp.override = io;
- Ext.override(sb, overrides);
- return sb;
- };
- }(),
- override : function(origclass, overrides){
- if(overrides){
- var p = origclass.prototype;
- for(var method in overrides){
- p[method] = overrides[method];
- }
- }
- },
- namespace : function(){
- var a=arguments, o=null, i, j, d, rt;
- for (i=0; i<a.length; ++i) {
- d=a[i].split(".");
- rt = d[0];
- eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
- for (j=1; j<d.length; ++j) {
- o[d[j]]=o[d[j]] || {};
- o=o[d[j]];
- }
- }
- },
- urlEncode : function(o){
- if(!o){
- return "";
- }
- var buf = [];
- for(var key in o){
- var ov = o[key], k = encodeURIComponent(key);
- var type = typeof ov;
- if(type == 'undefined'){
- buf.push(k, "=&");
- }else if(type != "function" && type != "object"){
- buf.push(k, "=", encodeURIComponent(ov), "&");
- }else if(ov instanceof Array){
- if (ov.length) {
- for(var i = 0, len = ov.length; i < len; i++) {
- buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
- }
- } else {
- buf.push(k, "=&");
- }
- }
- }
- buf.pop();
- return buf.join("");
- },
- urlDecode : function(string, overwrite){
- if(!string || !string.length){
- return {};
- }
- var obj = {};
- var pairs = string.split('&');
- var pair, name, value;
- for(var i = 0, len = pairs.length; i < len; i++){
- pair = pairs[i].split('=');
- name = decodeURIComponent(pair[0]);
- value = decodeURIComponent(pair[1]);
- if(overwrite !== true){
- if(typeof obj[name] == "undefined"){
- obj[name] = value;
- }else if(typeof obj[name] == "string"){
- obj[name] = [obj[name]];
- obj[name].push(value);
- }else{
- obj[name].push(value);
- }
- }else{
- obj[name] = value;
- }
- }
- return obj;
- },
- isIE : isIE,
- isIE7 : isIE7,
- isGecko : isGecko,
- isStrict:isStrict
- });
- })();
- Ext.applyIf(String, {
- escape : function(string) {
- return string.replace(/('|\\)/g, "\\$1");
- },
- /**
- var s = String.leftPad('123', 5, '0');
- // s now contains the string: '00123'
- */
- leftPad : function (val, size, ch) {
- var result = new String(val);
- if(ch === null || ch === undefined || ch === '') {
- ch = " ";
- }
- while (result.length < size) {
- result = ch + result;
- }
- return result;
- },
- /**
- var cls = 'my-class', text = 'Some text';
- var s = String.format('<div class="{0}">{1}</div>', cls, text);
- // s now contains the string: '<div class="my-class">Some text</div>'
- */
- format : function(format){
- var args = Array.prototype.slice.call(arguments, 1);
- return format.replace(/\{(\d+)\}/g, function(m, i){
- return args[i];
- });
- }
- });
- Ext.applyIf(Array.prototype, {
- indexOf : function(o){
- for (var i = 0, len = this.length; i < len; i++){
- if(this[i] == o) return i;
- }
- return -1;
- },
- remove : function(o){
- var index = this.indexOf(o);
- if(index != -1){
- this.splice(index, 1);
- }
- }
- });