Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Any way to clone an object?

I have an array of Titanium.UI.WebView objects.
I want to add them to a view and show that in the current window - this is working fine.

The problem is if I remove the container view and try to recreate it later using the original WebView array I get nothing (presumably because the array is full of pointers to null objects).

Here is some sample code to demonstrate:

Titanium.UI.setBackgroundColor('#fff');

function webv(vurl){
    return Titanium.UI.createWebView({url:vurl, borderWidth: 1, borderColor: '#000', height: 250, width: 250});   
}

var mainWin = Titanium.UI.createWindow({title: 'Web Test'}); 

var wvs = [];
wvs.push(webv('http://www.google.com'));
wvs.push(webv('http://www.google.com'));
wvs.push(webv('http://www.google.com'));
wvs.push(webv('http://www.google.com'));

var wvcontainer = Titanium.UI.createView({layout: 'horizontal'});
wvcontainer.add(wvs);

mainWin.add(wvcontainer);
mainWin.open();

//uncomment these to see the problem in action
//mainWin.remove(mainWin.children[0]);
//var wvcontainer2 = Titanium.UI.createView({layout: 'horizontal'});
//wvcontainer2.add(wvs);
//mainWin.add(wvcontainer2);

Is there a way to clone the original objects so I can use them again later? I could just hide the view, but I might want the WebView's in a different format (like a table for example).

Removing the view is killing reference to its children - how can I get around this?

Any advice would be appreciated.

— asked September 25th 2010 by Jon Keys
  • clone
  • object
0 Comments

6 Answers

  • I believe from a little research, you would have to use the "eval" function.

    http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript

    Pieces of the dynamic object or variable will be stored in arrays.
    You would peice the object together from the conjunction on a string and an increment
    This process would allow you to possibly find a variable to .hide or destroy it.

    //data would be an array
    //var i = 0;
    while(){
    eval("var img_" + i + "=123;");
    //store data into array "data"
    }
    
    — answered September 27th 2010 by Jason Brock
    permalink
    1 Comment
    • That would work, but then I would be recreating EVERY Javascript AND native object each time I want to reuse them. It would work in a pinch but it seems a little hacky. Thanks for your help!

      — commented September 28th 2010 by Jon Keys
  • I was trying to do this earlier, I could not figure it out. I hope someone can help clearify this! This would be a world of help!

    — answered September 25th 2010 by Jason Brock
    permalink
    0 Comments
  • i don't think so, because as i understand every Ti.* object has to have a corresponding Objective-c class-instance, so cloning would have to be done by a method on the objective-c side

    — answered September 25th 2010 by Christian Sigl
    permalink
    1 Comment
    • I agree … I see a new feature request in my future :-)

      — commented September 28th 2010 by Jon Keys
  • Maybe you could:

    var yourobject = ....;
    
    var save_obj = JSON.stringify(yourobject);
    
    ... do something ....
    
    var your_old_object = JSON.parse(save_obj);
    
    — answered September 27th 2010 by Carl Jahn
    permalink
    2 Comments
    • I don't think stringify works for native TI objects.

      — commented September 27th 2010 by Fabiano Taioli
    • The thing is the JS objects stay in tact, but the underlying native objects get garbage collected. So I have JS objects but they don't work.

      — commented September 28th 2010 by Jon Keys
  • function clone(o) {
     if(!o || ‘object’ !== typeof o)  {
       return o;
     }
     varc = ‘function’ === typeof o.pop ? [] : {};
     var p, v;
     for(p in o) {
     if(o.hasOwnProperty(p)) {
      v = o[p];
      if(v && ‘object’ === typeof v) {
        c[p] = clone(v);
      }
      else {
        c[p] = v;
      }
     }
    }
     return c;
    }
    
    — answered November 19th 2012 by Daniyar Supiev
    permalink
    0 Comments
  • Any ideas?

    — answered September 27th 2010 by Fabiano Taioli
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.