Created
October 30, 2008 19:53
-
-
Save koduki/21119 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package components { | |
import flash.utils.describeType; | |
import flash.utils.getDefinitionByName; | |
import mx.core.IFactory; | |
import mx.core.UIComponent; | |
import mx.events.FlexEvent; | |
import mx.utils.ObjectUtil; | |
public class CloneFactory implements IFactory{ | |
private var base:*; | |
private var init:Function | |
public function CloneFactory(base:UIComponent, init:Function = null) { | |
this.base = base; | |
this.init = init | |
} | |
public function newInstance(){ | |
var r:* = clone(this.base); | |
if (init != null){ | |
var bind:Function = function(site:Object, property:String, src:String = null):void{ | |
r.addEventListener(FlexEvent.DATA_CHANGE, function(e:FlexEvent):void{ | |
if(e.target.toString().split(/\./).pop() == "hiddenItem") return; | |
var value:* = (src) ? r.data[src] : r.data.toString(); | |
site[property] = value; | |
}) | |
} | |
init({"target":r, "bind":bind}) | |
} | |
return r; | |
} | |
public function clone(obj:*):*{ | |
var className:String = describeType(obj).attribute("name").toString() | |
var klass:Class = Class(getDefinitionByName(className)) | |
var r:* = new klass() | |
if(obj.hasOwnProperty("getChildren")){ | |
obj.getChildren().forEach(function(x:*, i:int, a:Array):void{ | |
r.addChild( clone(x) ) | |
}) | |
}else{ | |
for(var p:String in ObjectUtil.copy(obj)){ | |
var old:* = r[p] | |
if(obj[p] == null) continue | |
if(old != obj[p]) r[p] = obj[p] | |
} | |
} | |
return r | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment