Archive for July, 2010
* BlackBerry JavaScript Oddities
Posted on July 18th, 2010 by Dave Johnson. Filed under Uncategorized.
I have just discovered that the Element object in the BlackBerry browser (4.6+) supports the prototype object on some objects but not others. In particular the Element object supports it like this:
1 2 3 | Element.prototype.addEventListener = function() { alert('foo'); }; |
but the following does not work on the Document object:
1 2 3 | Document.prototype.addEventListener = function() { alert('bar'); }; |
What does work on the Document is overriding instance method like this:
1 2 3 | document.addEventListener = function() { alert('foobar'); }; |
Strange but true!
If you have any other BlackBerry tidbits please let me know since these old versions of BlackBerry browser are going to be the IE6 of the the mobile web.
* JavaScript Event Merging
Posted on July 14th, 2010 by Dave Johnson. Filed under JavaScript.
In a recent project I finally decided to write something to help with JavaScript event merging. There are often cases in JavaScript where you want some code to run but only after certain events have fired or other code run - in my case I wanted to execute a JavaScript function only after two different bits of code had executed. The most common use case for event merging I have come across has got to be on page load / DOM ready where you may need several conditions to be true before executing some JavaScript code.
Event merging generally helps you to better separate your concerns and not have unwieldy if conditions strewn throughout your code checking if certain condi.
If you have a basic JavaScript object called the “Subject” that has subscribe, subscribeOnce (events that auto-unsubscribe themselves after being fired the first time), and notify methods on them. You can use the following merge function to have all subscribed code executed only after each of the events passed to the merge function are fired. In this case I have explicitly used the subscribeOnce method on the event.
1 2 3 4 5 6 7 8 9 10 11 | function merge(handler, events) { var i = events.length; var f = function() { if (!(--i)) handler(); } for (var j=0; j<i; j++) { var e = events[j]; (!e.fired?e.subscribeOnce(f):i--); } if (!i) handler(); } |
When the merge function is called it iterates over all the events in the event array argument and checks if that event has already been fired or not by checking the “fired” property on the event - note that this makes it so that it doesn’t matter if merge is called before or after the events have fired. If the event has not been fired yet then a handler is attached using subscribeOnce where the handler checks a counter indicating how many of the events have been fired, once the counter hits 0 then the final handler for the merged events is called.
I have put up an entire event merging code on Github here.
Recent Posts
- Mobile Geolocation Implementations
- PhoneGap Native Bridge
- PhoneGap and BlackBerry OS 6
- BlackBerry JavaScript Oddities
- JavaScript Event Merging
Archives
- August 2010
- July 2010
- June 2010
- April 2010
- March 2010
- February 2010
- January 2010
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
