Etemplate - allow customisable onload for templates

This commit is contained in:
nathangray 2019-02-25 10:49:33 -07:00
parent 881f056c3c
commit 4a37e31102
2 changed files with 43 additions and 0 deletions

View File

@ -58,6 +58,12 @@ var et2_template = (function(){ "use strict"; return et2_DOMWidget.extend(
name: "URL of template", name: "URL of template",
type: "string", type: "string",
description: "full URL to load template incl. cache-buster" description: "full URL to load template incl. cache-buster"
},
"onload": {
"name": "onload",
"type": "js",
"default": et2_no_init,
"description": "JS code which is executed after the template is loaded."
} }
}, },
@ -195,6 +201,33 @@ var et2_template = (function(){ "use strict"; return et2_DOMWidget.extend(
return this.div; return this.div;
}, },
attachToDOM: function() {
if (this.div)
{
jQuery(this.div)
.off('.et2_template')
.bind("load.et2_template", this, function(e) {
e.data.load.call(e.data, this);
});
}
this._super.apply(this,arguments);
},
/**
* Called after the template is fully loaded to handle any onload handlers
*/
load: function() {
if(typeof this.options.onload == 'function')
{
// Make sure function gets a reference to the widget
var args = Array.prototype.slice.call(arguments);
if(args.indexOf(this) == -1) args.push(this);
return this.options.onload.apply(this, args);
}
},
/** /**
* Override to return the promise for deferred loading * Override to return the promise for deferred loading
*/ */

View File

@ -543,6 +543,16 @@ etemplate2.prototype.load = function(_name, _url, _data, _callback, _app, _no_et
jQuery(this.DOMContainer).trigger('load', this); jQuery(this.DOMContainer).trigger('load', this);
if(this.templates[this.name].attributes.onload)
{
var onload = et2_checkType(this.templates[this.name].attributes.onload.value,'js','onload');
if (typeof onload === 'string')
{
onload = et2_compileLegacyJS(onload, this, this.widgetContainer, this.widgetContainer);
}
onload.call(this.widgetContainer);
}
// Profiling // Profiling
if(egw.debug_level() >= 4) if(egw.debug_level() >= 4)
{ {