Have a great weekend.
Cheers,
Todd
Rantings & Ramblings on the Convergence of Code and Business
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init(event)">
<mx:Script>
<![CDATA[
import mx.events.DragEvent;
import flash.desktop.DragManager;
import flash.desktop.DragActions;
import flash.events.NativeDragEvent;
import flash.desktop.TransferableFormats;
import flash.filesystem.File;
private function init(e:Event):void
{
//add the event handlers
this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onEnter);
dropPanel.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDrop);
}
public function onEnter(event:NativeDragEvent):void
{
//Check to see if the drag item is the right format
if(event.transferable.hasFormat(TransferableFormats.FILE_LIST_FORMAT))
{
DragManager.acceptDragDrop(dropPanel);
}
}
public function onDrop(event:NativeDragEvent):void{
trace("dropped");
// Cast the drag & drop data as an array
var files:Array = event.clipboard.dataForFormat(flash.desktop.ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var f:File in files)
{
// check out the file URL
trace(f.url);
}
}
]]>
</mx:Script>
<mx:Panel id="dropPanel"
top="10"
left="10"
height="100"
width="100"
title="Drop Files Here"
backgroundColor="#FFF">
</mx:Panel>
</mx:WindowedApplication>
/**
* @author toddcullen
*/
CustomEvent = {};
CustomEvent.Events = {};
CustomEvent.Events.Base = Class.create();
CustomEvent.Events.Base.prototype = {
initialize : function(){
this.type = "CustomEvent.Events.Base";
}
}
CustomEvent.EventController = Class.create();
CustomEvent.EventController.prototype = {
initialize: function(){
this.listeners = $A([]);
},
addEventListener: function(n, f){
this.listeners.push({name: n, callback: f});
},
removeEventListener: function(n, f){
this.listeners = this.listeners.without({name: n, callback: f});
},
dispatchEvent: function(n, e){
for(var x=0; x<this.listeners.length; x++){
if(this.listeners[x].name == n){
this.listeners[x].callback(e);
}
}
}
}
var EventController = new CustomEvent.EventController();
/*
* Data Event
*/
CustomEvent.Events.DataEvent = Class.create();
CustomEvent.Events.DataEvent.prototype = {
initialize : function(data){
this.type = "CustomEvent.Events.DataEvent";
this.data = $H(data);
}
};
Event.observe(window, "load", function(){
EventController.addEventListener("BasicEvent", testListener);
EventController.addEventListener("DataEvent", testListener);
Event.observe('basic', 'click', dispatchBasic);
Event.observe('data', 'click', dispatchData);
});
function dispatchBasic(){
var event = new CustomEvent.Events.Base();
EventController.dispatchEvent("BasicEvent", event);
}
function dispatchData(){
var event = new CustomEvent.Events.DataEvent({info : 'HELLO WORLD!'});
EventController.dispatchEvent("DataEvent", event);
}
function testListener(event){
alert("event type:"+event.type);
if(event.type == "CustomEvent.Events.DataEvent"){
alert('event data: ' + event.data.inspect());
}
}
LocalServer Cache and serve application resources (HTML, JavaScript, images, etc.) locally | |
| Database Store data locally in a fully-searchable relational database |
| WorkerPool Make your web applications more responsive by performing resource-intensive operations asynchronously |
Custom Events are not a replacement for the Event object. Its really meant to aid in the loose coupling of the Model from the Controller in your client side code. Now that you've heard my spiel. Here's the code:
CustomEvent = {};
CustomEvent.Events = {};
CustomEvent.Events.Base = Class.create();
CustomEvent.Events.Base.prototype = {
initialize: function(){
this.listeners = new Array();
},
addEventListener: function(f){
this.listeners.push(f);
},
removeEventListener: function(f){
this.listeners = this.listeners.without(f);
},
dispatchEvent: function(n, d){
var data = this.setupData(d);
this.listeners.each(function(l){
l({name : n}, data);
});
},
setupData: function(d){
return $H(d);
}
}
CustomEvent.EventController = Class.create();
CustomEvent.EventController.prototype = {
initialize: function(){
this.events = new Hash();
},
create: function(n, t){
var args = arguments[2]?arguments[2]:null;
this.events[n] = new Event.CustomEvent[t](args);
},
addEventListener: function(n, f){
this.events[n].addEventListener(f);
},
removeEventListener: function(n, f){
this.events[n].removeEventListener(f);
},
dispatchEvent: function(){
var n = arguments[0];
var d = arguments[1] ? arguments[1] : {};
this.events[n].dispatchEvent(n, d);
},
destroy: function(n){
this.events.remove(n);
}
}
var EventController = new CustomEvent.EventController();
EventController.create("test", "Base");
EventController.addEventListener("test", myTest);
function myTest(evt){
alert('Event Name: '+evt.name);
}
EventController.dispatchEvent("test");