From df660bea3cf365d86d4606b37059c5f27c3e4e1f Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Wed, 9 Oct 2013 14:10:33 +0000 Subject: [PATCH] Better handling of doLoadingFinished and firing load event: - don't fire while still loading - prevent trying to load children before template is actually loaded from file --- etemplate/js/et2_widget_template.js | 34 ++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/etemplate/js/et2_widget_template.js b/etemplate/js/et2_widget_template.js index 846dfad751..82921c3a5a 100644 --- a/etemplate/js/et2_widget_template.js +++ b/etemplate/js/et2_widget_template.js @@ -76,6 +76,9 @@ var et2_template = et2_DOMWidget.extend( this._super.apply(this, arguments); this.div = document.createElement("div"); + + // Flag to indicate that loading is finished + this.loading = false; if (this.id != "" || this.options.template) { @@ -105,6 +108,8 @@ var et2_template = et2_DOMWidget.extend( if(splitted.length) { + // Still loading, don't fire loading finished + this.loading = true; et2_loadXMLFromURL(path, function(_xmldoc) { var templates = {}; // Scan for templates and store them @@ -116,9 +121,18 @@ var et2_template = et2_DOMWidget.extend( // Read the XML structure of the requested template this.loadFromXML(templates[template_name]); - - // Inform the widget tree that it has been successfully loaded. - this.loadingFinished(); + + // Update flag + this.loading = false; + + // If the parent is already attached, it has already + // finished loading. This template and its children + // were left out, so they need processing + if(!this.isAttached() && this._parent.isAttached()) + { + this.loadingFinished(); + } + }, this); } return; @@ -161,11 +175,15 @@ var et2_template = et2_DOMWidget.extend( * Override to trigger a load event, to facilitate processing when the xml file * is loaded asyncronously */ - doLoadingFinished: function() { - this._super.apply(this, arguments); - $j(this.getDOMNode()).trigger('load'); - return true; - } + doLoadingFinished: function() { + if(this.loading) return false; + this._super.apply(this, arguments); + var self = this; + window.setTimeout(function() { + $j(self.getDOMNode()).trigger('load'); + },0); + return true; + } }); et2_register_widget(et2_template, ["template"]);