1. #1
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,159
    Vote Rating
    322
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default Ext.ux.clone() - Object or Array cloning function

    Ext.ux.clone() - Object or Array cloning function


    Maybe I've reinvented the wheel..., anyway I haven't found a method of cloning objects or arrays in Ext.

    Theory:

    If you have an object or array in javascript and you assign this object to another variable only reference is copied but both variables point to same data. That comes very handy in many situations but there are cases when you really need clone of an array or an object, e.i. you need new fresh copy of data. Changing of data of the clone doesn't affect data of the original and vice versa.

    So, here it is:

    PHP Code:
    /**
     * Clone Function
     * @param {Object/Array} o Object or array to clone
     * @return {Object/Array} Deep clone of an object or an array
     * @author Ing. Jozef Sakáloš
     */
    Ext.ux.util.clone = function(o) {
        if(!
    || 'object' !== typeof o) {
            return 
    o;
        }
        if(
    'function' === typeof o.clone) {
            return 
    o.clone();
        }
        var 
    '[object Array]' === Object.prototype.toString.call(o) ? [] : {};
        var 
    pv;
        for(
    p in o) {
            if(
    o.hasOwnProperty(p)) {
                
    o[p];
                if(
    && 'object' === typeof v) {
                    
    c[p] = Ext.ux.util.clone(v);
                }
                else {
                    
    c[p] = v;
                }
            }
        }
        return 
    c;
    }; 
    // eo function clone 
    Usage is simple:

    PHP Code:

    var = {p:'text'p2:'text2'a:['a''b''c']};
    var 
    Ext.ux.clone(o); 
    I've done only brief tests so torture it please. (I haven't tested what happens if you pass funcion as the argument, I've tested only 1 level nested objects and 1 level nested arrays, seems to work for 'em)
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Blessing and Curse of refs, Grid MultiSearch Plugin


  2. #2
    Sencha User
    Join Date
    Nov 2007
    Location
    London, UK
    Posts
    583
    Vote Rating
    0
    mabello is on a distinguished road

      0  

    Smile


    Hi,
    first of all thanks a lot for ExtJS and for all your extension, all of them are really good and useful!!!!

    I had already implemented my Clone function that is really similar to your implementation,
    PHP Code:
    //Clone the obj 
    Util.Clone = function(myObj)
    {
      if(
    Util.isNullOrUndefined(myObj))
        return 
    myObj;
      var 
    objectClone = new myObj.constructor();
      for (var 
    property in myObj)
        if (
    typeof myObj[property] == 'object')
          
    objectClone[property] = Util.Clone(myObj[property]);
        else
          
    objectClone[property] = myObj[property];
      return 
    objectClone;

    but in one case I think your has some problem, try this:

    HTML Code:
    MyNamespace = {};
    
    MyNamespace.Person = function(){
    
        this.name = null;
        
        this.init = function(tName){
            //Add preoconditions
            if(tName == null || tName == "")//Add the preconditions u want...
                throw 'tName == null || tName == "" ';
            this.name = tName;
        }
        
        this.getName = function(){
            return this.name;
        }
        
        this.setName = function(value){
            this.name = value;
        }
    }   
    
    
    MyNamespace.Person2 = function(){
    
        var name = null;
        
        this.init = function(tName){
            //Add preoconditions
            if(tName == null || tName == "")//Add the preconditions u want...
                throw 'tName == null || tName == "" ';
            name = tName;
        }
        
        this.getName = function(){
            return name;
        }
        
        this.setName = function(value){
            name = value;
        }
    }
    
    MyNamespace.TreeNode = function(){
        
        this.parent = null;
        this.child = null;
        
        this.init = function(){
    
        }
        
        this.getParent = function(){
            return this.parent;
        }
        
        this.setParent = function(value){
            this.parent = value;
        }
        
        this.getChild = function(){
            return this.child;
        }
        
        this.setChild = function(value){
            this.child = value;
        }
    } 
      
    Util = {};
    
    Util.IsUndefined = function(a)
    {
         return (typeof a == 'undefined');
    } 
    
    Util.isNullOrUndefined = function(element)
    {
        return (Util.IsUndefined(element) || element == null );
    }
    
    //Clone the obj 
    Util.Clone = function(myObj)
    {
      if(Util.isNullOrUndefined(myObj))
        return myObj;
      var objectClone = new myObj.constructor();
      for (var property in myObj)
        if (typeof myObj[property] == 'object')
          objectClone[property] = Util.Clone(myObj[property]);
        else
          objectClone[property] = myObj[property];
      return objectClone;
    }
    
    Ext.onReady(function(){
    
        Ext.ux.clone = function(o) {
            if('object' !== typeof o) {
                return o;
            }
            var c = 'function' === typeof o.pop ? [] : {};
            var p, v;
            for(p in o) {
                v = o[p];
                if('object' === typeof v) {
                    c[p] = Ext.ux.clone(v);
                }
                else {
                    c[p] = v;
                }
            }
            return c;
        }  
        
        var marco = new MyNamespace.Person();
        marco.init('marco ');
        //Change the name for the cloned class
        var marcoCloned =  Ext.ux.clone(marco);
        marcoCloned.setName('marcoCloned');
        alert("marco.getName() is " + marco.getName());
        alert("marcoCloned.getName() is " + marcoCloned.getName());
        alert("Using Ext.ux.clone, marco.getName() == marcoCloned.getName(); Expected false, the result is " + (marco.getName() == marcoCloned.getName()));
        
        var mario = new MyNamespace.Person();
        mario.init('mario ');
        var marioCloned = Util.Clone(marco);
        //Change the name for the cloned class
        marioCloned.setName('marioCloned');
        alert("mario.getName() is " + mario.getName());
        alert("marioCloned.getName() is " + marioCloned.getName());
        alert("Using Util.Clone mario.getName() == marioCloned.getName(); Expected false, the result is " + (mario.getName() == marioCloned.getName()));
        
        //*****************
        var marco = new MyNamespace.Person2();
        marco.init('marco ');
        //Change the name for the cloned class
        var marcoCloned =  Ext.ux.clone(marco);
        marcoCloned.setName('marcoCloned');
        alert("marco.getName() is " + marco.getName());
        alert("marcoCloned.getName() is " + marcoCloned.getName());
        alert("Using Ext.ux.clone, marco.getName() == marcoCloned.getName(); Expected false, the result is " + (marco.getName() == marcoCloned.getName()));
        
        var mario = new MyNamespace.Person2();
        mario.init('mario ');
        var marioCloned = Util.Clone(marco);
        //Change the name for the cloned class
        marioCloned.setName('marioCloned');
        alert("mario.getName() is " + mario.getName());
        alert("marioCloned.getName() is " + marioCloned.getName());
        alert("Using Util.Clone, mario.getName() == marioCloned.getName(); Expected false, the result is " + (mario.getName() == marioCloned.getName()));
        //*****************
        
    });
    The case is when I use some private variable in a class and I expose them only through methods get and set.

    Both of us are failing with this:
    HTML Code:
        var parent = new MyNamespace.TreeNode();
        var child = new MyNamespace.TreeNode();
        parent.setChild(child);
        child.setParent(parent);
        
        var parentClone1 =  Ext.ux.clone(parent);
        
        var parentClone2 = Util.Clone(parent);
        
        alert(parentClone1.getChild());
        alert(parentClone2.getChild());
    I haven't check yours, but mine is because a reference b, which reference a and so on, I already know this problem, but I'm lazy...I have not stressed a lot also my implementation I have to say...
    What about a merge?
    Thanks for sharing and keep up the great work

  3. #3
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,159
    Vote Rating
    322
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default


    The clone function is in no case meant to clone classes or instantiated Ext objects. It is almost impossible as these install event handlers almost always so cloning would definitely lead to unpredictable results.

    The clone function is meant for cloning simple (multi-nested) object or arrays. For example, if you need to clone baseParams object or a data array for stores or similar.
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Blessing and Curse of refs, Grid MultiSearch Plugin


  4. #4
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,159
    Vote Rating
    322
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default


    BTW, it's not possible to clone objects with private vars if they don't provide special methods for that...
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Blessing and Curse of refs, Grid MultiSearch Plugin


  5. #5
    Sencha User
    Join Date
    Nov 2007
    Location
    London, UK
    Posts
    583
    Vote Rating
    0
    mabello is on a distinguished road

      0  

    Default Agree and disagree

    Agree and disagree


    Are you sure about the private variable of a class?
    I think with my Util.Clone function seems to work fine also in this case..

    Anyway, I agree with you that the clone of a "complex" Ext object could be dangerous, but my clone method is born before ExtJS, so for a normal business object class works

    I'd like to try with an Exj object, just curious...for extJS object, to avoid the problem could be good to implement a clone method, like in .NET where you can implement the interface IClonable with the clone method...I think it is the best solution

    Thanks, you've been really kind and keep up the good work!

  6. #6
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,159
    Vote Rating
    322
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default


    Re private vars:

    PHP Code:
    var = function() {
        var 
    priv 4;

        return {
            
    pub:44;
        };
    }(); 
    You have no access to priv from outside so you cannot clone o from outside.

    Re cloning Ext objects: Trying to clone them is just waste of time.

    Re .NET: What is it? I do not use Mirco$oft products.
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Blessing and Curse of refs, Grid MultiSearch Plugin


  7. #7
    Sencha User
    Join Date
    Nov 2007
    Location
    London, UK
    Posts
    583
    Vote Rating
    0
    mabello is on a distinguished road

      0  

    Default


    Sorry, I was trying to have an idea for the cloning of an Ext obj, I don't care about it, I'm not using my clone method to clone any ExtJS in my code, I usually clone the configObj like you.

    Let's say that you can extend an ExtJS obj by implementing a method clone like a class method, and the clone method has the responsability to clone the object in the right way, instead of using an utility (static) method like mine or yours clone.

    An idea could be to mantain a reference to the configObj as a class field and use it to create a new cloned object and then synchronize the "state" between the new cloned object and the old one...

    .NET was an example...Cloneable in java is another example, this interface has the same responsability...it's not important the language, I was only trying to give an example...

    Besides, I was talking about my "javascript class" in my example and about the "private" variable name

    HTML Code:
    MyNamespace.Person2 = function(){
        var name = null;
        
        this.init = function(tName){
            //Add preoconditions
            if(tName == null || tName == "")//Add the preconditions u want...
                throw 'tName == null || tName == "" ';
            name = tName;
        }
        
        this.getName = function(){
            return name;
        }
        
        this.setName = function(value){
            name = value;
        }
    }
    var name is a private variable...

    Thanks

  8. #8
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,159
    Vote Rating
    322
    jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future jsakalos has a brilliant future

      0  

    Default


    Quote Originally Posted by mabello View Post
    Let's say that you can extend an ExtJS obj by implementing a method clone like a class method, and the clone method has the responsability to clone the object in the right way, instead of using an utility (static) method like mine or yours clone.

    An idea could be to mantain a reference to the configObj as a class field and use it to create a new cloned object and then synchronize the "state" between the new cloned object and the old one...

    Yes, this would be possible. Maybe I'll think of it when writing sth in the future. I have no idea of pros and cons of such approach; I've never tried it...
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Blessing and Curse of refs, Grid MultiSearch Plugin


  9. #9
    Sencha User
    Join Date
    Nov 2007
    Location
    London, UK
    Posts
    583
    Vote Rating
    0
    mabello is on a distinguished road

      0  

    Default


    Thanks again for your kind answer, ExtJS rocks! :-)

  10. #10

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar