Past two weeks, I’ve been porting over some Flashcom & Flex 1.5 code to Flex 2 (still using FCS 1.5 vs. FMS 2). One nice addition I found in the ActionScript 3 SharedObject class is the client property. This little gem allows you have events that are called by SharedObject.send to be called on a proxy class instead. What this means is, if someone in a Flashcom application causes the remote SharedObject to be updated, and it fires a “onChatMessageUpdate” method, it’ll actually get called on the client object if you set it to something. Since a lot of intrinsic classes are now “final”, meaning you can’t futz with ‘em as easily as you could in AS2, this is a great way to incorporate it via Composition.
var my_rso:SharedObject = SharedObject.getRemote("chat", nc.uri, false); my_rso.owner = this; my_rso.onChat = function(str) { this.owner.onChatMessage(str); }; function onChatMessage(str) { textArea.text += str; }
In the past, you’d use something like the above; proxying the message yourself, or even putting the logic inside the method closure if you were in a hurry.
Now, you can forward it to an anonymous object, yourself, or even a class instance.
var my_rso:SharedObject = SharedObject.getRemote("chat", nc.uri, false); my_rso.client = this; // onChatMessage needs to be a public function in this class my_rso.client = {onChatMessage: function(str){ // }}; // anonymous function my_rso.client = new ProxyClass(); // you can make a class specifically to handle events
David Simmons from Adobe told me it was on NetConnection as well. Just checked, and she’s on NetStream too. In my tests, for classes, it only works on methods in the public namespace; protected & private don’t work. I didn’t test custom namespaces, nor do I know how to make it “see” them.
Handy! Thanks Adobe.