/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package { import flash.utils.Proxy; import flash.utils.describeType; import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; import flash.utils.flash_proxy; // import flash.utils.flash_proxy; public dynamic class ActiveRecord extends Proxy { private static var functionsToAdd:Array = ["find_by_name","find_by_id","find_all"]; protected static var methodFactory:DynamicMethodFactory; public function ActiveRecord() { } public static function staticInitializer(klass:Class):void{ var typeInfo:XML = describeType(klass); for each(var s:String in functionsToAdd){ klass[s] = getMethod(typeInfo.@name,s); } } public static function getMethod(objectName:String,methodName:String):Function{ return function(...args):Object{return endPoint(objectName,methodName,args);}; } public static function endPoint(objectName:String,methodName:String,...args):Object{ trace("You called: " + objectName + "." + methodName + "(" + args + ")"); var klass:Class = getDefinitionByName(objectName) as Class; var o:ActiveRecord = new klass(); return o; } flash_proxy override function callProperty(method: *, ...args): * { try { var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class; return clazz.prototype[method].apply(method, args); } catch (e : Error) { return methodMissing (method, args); } } protected function methodMissing(method : *, args : Array) : Object{ throw( new Error("Method Missing")); return null; } } }