[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.
This commit is contained in:
qsm-odoo
2017-07-24 13:41:50 +02:00
parent 417a664f16
commit dbfdd86cc2
+21 -3
View File
@@ -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();
},
/**