Posts

Showing posts with the label watch

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( ...

Internet Explorer Object watch

Update As Luke suggested, there is a method remove in the watcher that should solver leaks problem while the new version should be supported by most recent Opera, Safari, FireFox, and Internet Explorer. Main updates: the watcher is always another object and not the original one to safely destroy the watcher use its destroy method and then delete it ---------------------------------------------- We all complain about IE and its non-standard attitude, this time I would like to introduce a non standard feature, as Object.prototype.watch is, for Internet Explorer. What is watch and why we need it The watch method allows validation, control, monitoring, over a generic object. This means that we can control properties assignment with one, or more, callbacks. var man = {}; man.watch("name", function(propertyName, oldValue, newValue){ // propertyName: name // oldValue: undefined if it is the first time, the old one otherwise // newValue: new assigned valu...