I would like to watch a DOM node property but I can’t seem to get it to work.
Within my widget, I’ve tried the following.
startup: function() {
this.inherited(arguments);
// First try using the dojo 1.6 watch.
// I'm setting the property of the widget
// to reference the DOM node's offsetWidth property
this.width = this.domNode.offsetWidth;
this.watch("width", function() {
console.debug('Width changed to ' + this.domNode.offsetWidth )
})
// Does this.width contain a reference or a copy of this.domNode.offsetWidth?
// Second try, the Mozilla watch
this.domNode.watch("offsetWidth", function() {
console.debug('Width changed to ' + this.domNode.offsetWidth )
})
}
You can’t get it to work by adding Mozilla’s
watchdirectly to the DOM node.At http://james.padolsey.com/javascript/monitoring-dom-properties/ mention is made of using
setInterval,onpropertychange(IE only), andDOMSubtreeModified(there are other such standard DOM modification events likeDOMAttrModified, but you’d have to check browser support). The standard DOM modification events only work if the DOM attribute is changed, not the equivalent property (though you can trigger a mutation event from JS byinitMutationEvent):You can monitor the setting of the width property here using the Dojo API, but this does not appear to track the DOM node for you (though http://dojotoolkit.org/features/1.6/widget-watch seems to suggest it does). For example, you can do:
and then your
watchevent above could be modified to dynamically change the DOMwidth(but not theoffsetWidthsince that is a read-only property).Still, it appears you’re trying to detect automatic
offsetWidthcalculation changes, not your own changes. The best solution for you at this point seems to me to besetInterval.A copy since this.domNode.offsetWidth is a number and in JavaScript, non-object types will always get copied by value.
If this were able to work (and it doesn’t), you’d need to use
this.offsetWidthinside the function since Mozilla sets the callbackthisto that of the object being watched.