Ext. util. Mixed Collection (1) 의 기본 동작 해독
Ext. util. Mixed Collection - Ext 는 비교적 횡포 한 사용자 정의 데이터 구조 로 Store 를 구축 하 는 기초 이 고 Store 는 Ext 의 데이터 처리 층 이 며 상층 UI 를 구축 하 는 기반 이다.
저 는 Ext. util. Mixed Collection 을 map 와 배열 의 합 체 로 보고 실현 하 는 방법 도 간단 합 니 다. 바로 map 와 배열 에 각각 데 이 터 를 저장 하 는 것 입 니 다.
Ext.util.MixedCollection = function(allowFunctions, keyFn){
    //  value     
    this.items = [];
    //  key—value,         
    this.map = {};
    //  key   
    this.keys = [];
    //  ,              
    this.length = 0;
    //       ,             
    this.addEvents(
        "clear",
        "add",
        "replace",
        "remove",
        "sort"
);
 his.allowFunctions = allowFunctions === true;
    //    key 
    if(keyFn){
        this.getKey = keyFn;
    }
   //         
    Ext.util.MixedCollection.superclass.constructor.call(this);
};
Ext.util.MixedCollection   Ext.util.Observabl  ,       “  ” ,                    
Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {.......},
 //    
 add: function(key, o){
        //        
        //value      ,key value id 
       if(arguments.length == 1){
            
            o = arguments[0];
            key = this.getKey(o);
        }
        if(typeof key != 'undefined' && key !== null){
            var old = this.map[key];
            if(typeof old != 'undefined'){
                return this.replace(key, o);
            }
            this.map[key] = o;
        }
        //            
       //  +1
        this.length++;
       //value  items
        this.items.push(o);
       //key  keys
        this.keys.push(key);
        //    add  ,add        
        this.fireEvent('add', this.length-1, o, key);
        //  value
        return o;
    },
    //  
    replace : function(key, o){
        // add
        if(arguments.length == 1){
            o = arguments[0];
            key = this.getKey(o);
        }
        var old = this.map[key];
        //     key
        if(typeof key == "undefined" || key === null || typeof old == "undefined"){
             //     
             return this.add(key, o);
        }
        //  key   
        var index = this.indexOfKey(key);
        //  items
        this.items[index] = o;
        //  map
        this.map[key] = o;
        //  replace  
        this.fireEvent("replace", key, old, o);
        return o;
    },
//               
addAll : function(objs){
        //              
        if(arguments.length > 1 || Ext.isArray(objs)){
            var args = arguments.length > 1 ? arguments : objs;
            for(var i = 0, len = args.length; i < len; i++){
                //add  
                this.add(args[i]);
            }
        }else{
            for(var key in objs){
                //                  
                if(this.allowFunctions || typeof objs[key] != "function"){
                    this.add(key, objs[key]);
                }
            }
        }
    },
 //  ,       
 //      ,    remove  ,     
//       items   
 each : function(fn, scope){
        //  items  
        //        
        var items = [].concat(this.items); 
        for(var i = 0, len = items.length; i < len; i++){
            //     fn    false      
            //           
            //           ,     false,    
            if(fn.call(scope || items[i], items[i], i, len) === false){
                break;
            }
        }
    },
//             
eachKey : function(fn, scope){
        for(var i = 0, len = this.keys.length; i < len; i++){
            fn.call(scope || window, this.keys[i], this.items[i], i, len);
        }
 },
//   ,   fn    true    
    find : function(fn, scope){
        for(var i = 0, len = this.items.length; i < len; i++){
            if(fn.call(scope || window, this.items[i], this.keys[i])){
                return this.items[i];
            }
        }
        return null;
    },
    //    
    insert : function(index, key, o){
        //     2   ,     
        if(arguments.length == 2){
            //o     key     
            o = arguments[1];
            //key  getKey   ,   o id
            key = this.getKey(o);
        }
        //        ?
        //         ,            
        //       ,  insert   , key     ,               
        //ext       ,   
        //       ,        remove  
        //   add     add  
        //               add  ,   remove  
        //          ,           add  。
        if(this.containsKey(key)){
            //    
            this.suspendEvents();
            //     removerByKey   
            //    key-value 
            this.removeKey(key);
            //    
            this.resumeEvents();
        }
        if(index >= this.length){
            return this.add(key, o);
        }
        this.length++;
        this.items.splice(index, 0, o);
        if(typeof key != "undefined" && key !== null){
            this.map[key] = o;
        }
        this.keys.splice(index, 0, key);
        this.fireEvent("add", index, o, key);
        return o;
    },
//  
clear : function(){
        this.length = 0;
        this.items = [];
        this.keys = [];
        this.map = {};
        this.fireEvent("clear");
    },
//     ,   ,       start end Array(items)
getRange : function(start, end){
        var items = this.items;
        if(items.length < 1){
            return [];
        }
        //    
        start = start || 0;
        //    ,      ,      
        end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
        var i, r = [];
        if(start <= end){
            for(i = start; i <= end; i++) {
                r[r.length] = items[i];
            }
        }else{
            for(i = start; i >= end; i--) {
                r[r.length] = items[i];
            }
        }
        return r;
    },
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.