Tuesday, May 6th, 2008

Porting Dojo Methods to Flash

Category: Dojo, Flash

Mike Wilcox has started a nice series of posts on porting Dojo methods to Flash as a homage for Open Screen (aside: I applaud Adobe’s intentions, but need to see a non-assert of their IP before I can do anything with it.)

In part one of the series Mike ports dojo.hitch to ActionScript:

javascript

  1. _global.lang = {
  2.     hitch: function(scope, method){
  3.         if(!method){
  4.             method = scope;
  5.             scope = _global;
  6.         }
  7.         var args;
  8.         if(arguments.length>2){
  9.             args = arguments;
  10.             args.shift();
  11.             args.shift();
  12.         }
  13.         if(typeof(method) == "string"){
  14.             return  function(){ scope[method].apply(scope, args) };
  15.         }
  16.         return function(){ method.apply(scope, args) };
  17.     }
  18. }

In part two he adds support for dojo.connect(), resulting in:

javascript

  1. _global.lang = {
  2.     __conListeners:{},
  3.     __id:0,
  4.     connect: function(source, event, target, callback){
  5.         var hitched = this._hitch(target, callback);
  6.  
  7.         var listener = new Object();
  8.         listener[event] = function(args){
  9.             hitched();
  10.         }
  11.  
  12.         if(!source.broadcasters) {
  13.             source.broadcasters = {};
  14.             AsBroadcaster.initialize(source)
  15.         }
  16.  
  17.         source.addListener(listener);
  18.  
  19.         if(!source.broadcasters[event]){
  20.             source[event] = function(){
  21.                 source.broadcastMessage(event, arguments);
  22.             }
  23.             source.broadcasters[event] = true;
  24.         }
  25.  
  26.         var id = "connect_"+this.__id++;
  27.         this.__conListeners[id] = {listener:listener, source:source};
  28.         return id;
  29.     }
  30.  
  31.     disconnect: function(id){
  32.         var con = this.__conListeners[id];
  33.         if(con){
  34.             con.source.removeListener(con.listener);
  35.             con = null;
  36.         }
  37.     }
  38. }

Posted by Dion Almaer at 7:12 am
Comment here

++++-
4.1 rating from 19 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.