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.
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" }
-
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!
-
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
-
Maybe you could:
var yourobject = ....; var save_obj = JSON.stringify(yourobject); ... do something .... var your_old_object = JSON.parse(save_obj);
-
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; }
-
Any ideas?