Change comment:
Example of proper initialization using xwiki:dom:loaded and xwiki:dom:updated
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -33,7 +33,22 @@ 33 33 * **##xwiki:dom:loading##** 34 34 ##xwiki:dom:loading## is sent between ##dom:loaded## and ##xwiki:dom:loaded##, before XWiki changes the DOM. This is the event that should start all scripts making important DOM changes that other scripts should see. 35 35 * **##xwiki:dom:updated##** 36 -This event is sent whenever an important change in the DOM occurs, such as loading new content in a dialog box or tab, or refreshing the document content. Scripts that add behavior to certain elements, or which enhance the DOM, should listen to this event as well and re-apply their initialization process on the updated content, the same way that the whole DOM is enhanced on ##xwiki:dom:loaded##. The list of new or updated elements is sent in the ##event.memo.elements## property. 36 +This event is sent whenever an important change in the DOM occurs, such as loading new content in a dialog box or tab, or refreshing the document content. Scripts that add behavior to certain elements, or which enhance the DOM, should listen to this event as well and re-apply their initialization process on the updated content, the same way that the whole DOM is enhanced on ##xwiki:dom:loaded##. The list of new or updated elements is sent in the ##event.memo.elements## property. For example:((( 37 +{{code language="javascript"}} 38 +var init = function(elements) { 39 + // Search for special content to enhance in each DOM element in the "elements" list and enhance it 40 + elements.each(function(element) { 41 + element.select('.someBehavioralClass').each(function(item) { 42 + enhance(item); 43 + }) 44 + } 45 +} 46 +['xwiki:dom:loaded', 'xwiki:dom:updated'].each(function(eventName) { 47 + document.observe(eventName, function(event) { 48 + init(event.memo && event.memo.elements || [document.documentElement]); 49 + } 50 +}); 51 +{{/code}} 37 37 38 38 {{warning}} 39 39 If your script is loaded **deferred**, all these events may be triggered **before your script is executed** and therefore **before it has the ablity to observe these events**. Since 3.1.1, to prevent your handler to never being called, never use ##dom:loaded## anymore, and check ##XWiki.isInitialized## before waiting for ##xwiki:dom:loading##, and ##XWiki.domIsLoaded## before waiting for ##xwiki:dom:loaded##. If the flag is true, you should proceed immediately with your handler. Here is a simple construct to properly handle this:(((