Posts

Showing posts with the label LiveMonitor

One Function To Trap Them All

This is a quick one. This technique could have some side-effect I am not aware about but I've never seen it so far in any library. Talking about LiveMonitor I have successfully tested same concept to create faster implementation of some of my DOM common code, not yet updated in vice-versa . document.getElementsByClassName Example (function(childNodes){ /* Another (C) WebReflection Silly Idea */ // how to re-use them all, just an example if(!document.getElementsByClassName) document.getElementsByClassName = function getElementsByClassName(className){ for(var re = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"), ret = [], i = 0, l = 0, length = childNodes.length, node; i < length; ++i ){ if((node = childNodes[i]).nodeType === 1 && re.test(node.className)) ret[l++] = node ; }; return ret; } ; // let the magic happen })(document.getElemen...

LiveMonitor - Asynchronous Property Monitor

Today I would like to introduce you a quite uncommon JavaScript trick , a trapped Live Object or, generally speaking, a lightweight monitor able to understand when a generic property has been changed. About Live Objects A live object could be described as a particular object able to change without our interaction. The most common live object example is this: // this is the most common live object // the HTMLCollection var divs = document.getElementsByTagName("div"); divs.length; // let's say 4 // let's add another div inside a generic node document.body.appendChild( document.createElement("div") ); divs.length; // 5! In few words DOM searches are dynamic, which is the reason almost every selector library needs to transform the current result into a static Array . About LiveMonitor Specially suited for live objects, LiveMonitor is a function which aim is to notify us when the specified property change: // LiveMonitor example var lm = new LiveMonitor( ...