Complex Javascript Event Handling: EventMediator

A large enterprise sized project I work on uses YUI library extensively, and events are a huge part of the rich front end we’re developing. What you start to learn quickly about UI events, is that dependencies between different events start to get very complicated very fast. When A depends on B, but B needs to wait for C and D and E to finish, but E needs to wait for F to finish, you have a complex situation on your hands. Up until recently we were able to rely mostly on just using CustomEvents in YUI to handle this for us.

Where that starts to break down is when you have one event, A, that is dependent on multiple other events, B, C, D. You now have to manually keep track of what has fired, and make sure you don’t fire that A until B, C and D have all fired. With very little code, here is a simple class that can be used in conjunction with YIU CustomEvents. It will handle the ‘book-keeping’ of firing what you want when all the events you have designated fire.

EventMediator = {
	addActivationRecord : function(record) {
		record = record || {};
		var that = this;
		for(var idx in record.events) {
			var eventRecord = record.events[idx];
			if(eventRecord.event !== null) {
				eventRecord.fired = false;
				eventRecord.event.subscribe(function(scopedEvent) {
					return function(e) {
						scopedEvent.fired = true;
						that.fireActivation(record);
					}
				}(eventRecord));
			}
		}
	},
	fireActivation : function(record) {
		var fired = true;
		for(var idx in record.events) {
			if(record.events[idx].fired === false) {
				fired = false;
				break;
			}
		}
		if(fired === true) {
			record.activate.call(record.scope);
		}
	}
};

Using the EventMediator would look something like the following:

EventMediator.addActivationRecord({
	events : [
		{ event : myObj.someYUICusomEvent },
		{ event : myObj.anotherYUICusomEvent }
	],
	activate : myObj1.myActivationCallback,
	scope : myObj1
});

It’s pretty straightforward I think. You call addActivationRecord, passing in an array of objects, whose ‘event’ property points to a YUI CustomEvent. You also provide an activate callback method, and give it a scope in which to call the method. For my purposes so far this has worked pretty well, although I’m sure it could be built up to be much more robust. Hope it helps someone out!

  • del.icio.us
  • Digg
Tagged , | 4 Comments

Subscribing to an image loaded event

I recently was working on something where I needed to swap the src of an image, but then perform some specific javascript AFTER the new image was loaded, namely resize the dimensions a little. This may seem pretty simple, but took me a little research, along with trial and error to get a working solution, so hopefully it helps someone else. I often use jQuery on my smaller projects, so I’ll show how I accomplished this with that toolkit.

  1. First, I would need to listen to the load event of the image I was replacing the src of.
  2. Next, in order to trigger the handler, we need to actually change the src of the image
$('#my-image').load(function() {
    //Logic for w/e here, resizing, etc.
}).attr('src', myNewImageSrc);

The code itself is really simple, but it took me a few to realize that jQuery solved the problem by simply letting us listen to the load event of the image, and count on it being fired when the src changes, and finishes loading.

  • del.icio.us
  • Digg
Tagged , | Leave a comment

slickifying Slikcalc to version 1.0

A new version of Slikcalc (1.0) was released. This release simplified some code, mainly the slikcalc.formatCurrency() method. There was also a bug where a calculator could be initialized more than once depending on timing that was fixed. As always, feel free to leave feedback on the library. Enjoy.

Download Slikcalc 1.0

  • del.icio.us
  • Digg
Tagged , | Leave a comment

Format Currency in Javascript (simplified)

While working on Slikcalc, I was trying to find the easiest way to format a number for currency. I had found some implementations that were pretty complex, using regex and absolute values, and thought there had to be a simpler way. This is what I came up with and so it works great in all the browsers I’ve tested (IE 6/7, Firefox 2 (windows & mac), Opera 9 (windows and mac).

function formatCurrency(num) {
	num = isNaN(num) || num === '' || num === null ? 0.00 : num;
	return parseFloat(num).toFixed(2);
}

Just thought I’d share for anyone working with something similar.

  • del.icio.us
  • Digg
Tagged , | 12 Comments

Slikcalc – Easy Javascript Calculations



Slikcalc - Easy Javascript Calculations

I’ve put together a small javascript library that greatly simplifies creating dynamic javascript calculators. It works great for just about anything, ranging from calculating columns of values, to a complex custom formula. You can even write your own calculators and plug them into the framework with little effort. Calculations update automatically as the user types in their values.

Check out the details on the library, and download it here

Check out an example below, and the code that goes along with it

Amount One:
Amount Two: 7.29
Amount Three:
  Total:  
var columnCalc1 = new slikcalc.ColumnCalc({
	total: { id: 'cc-1-total' },
	registerListeners: true,
	calcOnLoad: true
});
columnCalc1.addRow({ id: 'cc-1-1' });
columnCalc1.addRow({ id: 'cc-1-2' });
columnCalc1.addRow({
	id: 'cc-1-3',
	checkbox: { id: 'cc-1-3-c' }
});

A few key features of Slikcalc:

  • Slikcalc works with popular Javascript libraries, including YUI, jQuery, Dojo, Mootools and Prototype. Adding support for new libraries is very simple as well.
  • Slikcalc handles attaching your event listeners for when the users type in values, click checkboxes, and when the page loads.
  • Slikcalc provides a simple interface for chaining multiple calculators so one can fire another, and so on.
  • del.icio.us
  • Digg
Tagged , | 16 Comments

Page optimized by WP Minify WordPress Plugin