July 30, 2009

JavaScript Custom Event

var CustomEvent = function(){
//name of the event
this.eventName = arguments[0];
var mEventName = this.eventName;

//function to call on event fire
var eventAction = null;

//subscribe a function to the event
this.subscribe = function(fn){
eventAction = fn;
};

//fire the event
this.fire = function(sender, eventArgs){
this.eventName = eventName2;
if(eventAction != null){
eventAction(sender, eventArgs);
}
else {
alert('There was no function subscribed to the ' + mEventName + ' event!');
}
};
};
========= example
var myEvent = new CustomEvent("my event");
myEvent.subscribe(function(sender, eventArgs){
alert(eventArgs.message);
});
========= Fire the event
myEvent.fire(null, {
message: 'you just witnessed the firing of a custom event called ' + this.eventName + '!'
});


Reference

July 19, 2009

Function as Data in Javascript

function multiplyByTwo(a, b, c, callback){
var i, ar = [];
for(i = 0; i < 3; i++){
ar[i] = callback(arguments[i] * 2);
}
return ar;
}

function addOne(value){
return value + 1;
}

multiplyByTwo(2,4,6, addOne);

July 06, 2009

CSS float for firefox and IE

Dom.styleFloat // for IE
Dom.cssFloat // for firefox