From dbfdd86cc2c49abdebfb3b31d6233f11368feb49 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Thu, 29 Jun 2017 16:38:34 +0200 Subject: [PATCH] [IMP] web: add a widget property which allows to lazy load xml files Add the 'xmlDependencies' key to the 'Widget' class. If defined, it is an array of URLs to XML files that the widget will need to work. Those are loaded in the `willStart` method so that they are available before all rendering parts. --- addons/web/static/src/js/core/widget.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/core/widget.js b/addons/web/static/src/js/core/widget.js index 82e8041c98a..8ef141a6020 100644 --- a/addons/web/static/src/js/core/widget.js +++ b/addons/web/static/src/js/core/widget.js @@ -1,6 +1,7 @@ odoo.define('web.Widget', function (require) { "use strict"; +var ajax = require('web.ajax'); var core = require('web.core'); var mixins = require('web.mixins'); var ServicesMixin = require('web.ServicesMixin'); @@ -69,6 +70,15 @@ var Widget = core.Class.extend(mixins.PropertiesMixin, ServicesMixin, { * @type string */ template: null, + /** + * List of paths to xml files that need to be loaded before the widget can + * be rendered. This will not induce loading anything that has already been + * loaded. + * + * @type {string[]} + */ + xmlDependencies: null, + /** * Constructs the widget and sets its parent if a parent is given. * @@ -94,13 +104,21 @@ var Widget = core.Class.extend(mixins.PropertiesMixin, ServicesMixin, { this.setElement(this._make_descriptive()); }, /** - * Method called between init and start. Performs asynchronous calls required by start. + * Method called between @see init and @see start. Performs asynchronous + * calls required by the rendering and the start method. * - * This method should return a Deferred which is resolved when start can be executed. + * This method should return a Deferred which is resolved when start can be + * executed. * - * @return {jQuery.Deferred} + * @returns {Deferred} */ willStart: function () { + if (this.xmlDependencies) { + var defs = _.map(this.xmlDependencies, function (xmlPath) { + return ajax.loadXML(xmlPath, core.qweb); + }); + return $.when.apply($, defs); + } return $.when(); }, /**