From 81e028b791d626588f949360adf2d2f12b8ffbb9 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Wed, 14 Aug 2013 18:43:22 +0200 Subject: [PATCH] [ADD] QWeb: add async api (backward compatible) bzr revid: fme@openerp.com-20130814164322-cny24d0i6tg4ftvn --- addons/web/static/lib/qweb/qweb2.js | 94 ++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/addons/web/static/lib/qweb/qweb2.js b/addons/web/static/lib/qweb/qweb2.js index 52e975322e5..4512b749682 100644 --- a/addons/web/static/lib/qweb/qweb2.js +++ b/addons/web/static/lib/qweb/qweb2.js @@ -226,10 +226,27 @@ QWeb2.Engine = (function() { } QWeb2.tools.extend(Engine.prototype, { - add_template : function(template) { + /** + * Add a template to the engine + * + * @param {String|Document} template Template as string or url or DOM Document + * @param {Function} [callback] Called when the template is loaded, force async request + */ + add_template : function(template, callback) { + var self = this; this.templates_resources.push(template); if (template.constructor === String) { - template = this.load_xml(template); + return this.load_xml(template, function (err, xDoc) { + if (err) { + if (callback) { + return callback(err); + } else { + throw err; + } + } + self.add_template(xDoc, callback); + }); + template = this.load_xml(template, callback); } var ec = (template.documentElement && template.documentElement.childNodes) || template.childNodes || []; for (var i = 0; i < ec.length; i++) { @@ -262,35 +279,56 @@ QWeb2.Engine = (function() { } } } + if (callback) { + callback(null, template); + } return true; }, - load_xml : function(s) { + load_xml : function(s, callback) { + var self = this; + var async = !!callback; s = this.tools.trim(s); if (s.charAt(0) === '<') { - return this.load_xml_string(s); + var tpl = this.load_xml_string(s); + if (callback) { + callback(null, tpl); + } + return tpl; } else { var req = this.get_xhr(); - if (req) { - // TODO: third parameter is async : https://developer.mozilla.org/en/XMLHttpRequest#open() - // do an on_ready in QWeb2{} that could be passed to add_template - if (this.debug) { - s += '?debug=' + (new Date()).getTime(); // TODO fme: do it properly in case there's already url parameters - } - req.open('GET', s, false); - req.send(null); - var xDoc = req.responseXML; - if (xDoc) { - if (!xDoc.documentElement) { - throw new Error("QWeb2: This xml document has no root document : " + xDoc.responseText); - } - if (xDoc.documentElement.nodeName == "parsererror") { - return this.tools.exception(xDoc.documentElement.childNodes[0].nodeValue); - } - return xDoc; - } else { - return this.load_xml_string(req.responseText); - } + if (this.debug) { + s += '?debug=' + (new Date()).getTime(); // TODO fme: do it properly in case there's already url parameters } + req.open('GET', s, async); + if (async) { + req.onreadystatechange = function() { + if (req.readyState == 4) { + if (req.status == 200) { + callback(null, self._parse_from_request(req)); + } else { + callback(new Error("Can't load template, http status " + req.status)); + } + } + }; + } + req.send(null); + if (!async) { + return this._parse_from_request(req); + } + } + }, + _parse_from_request: function(req) { + var xDoc = req.responseXML; + if (xDoc) { + if (!xDoc.documentElement) { + throw new Error("QWeb2: This xml document has no root document : " + xDoc.responseText); + } + if (xDoc.documentElement.nodeName == "parsererror") { + throw new Error("QWeb2: Could not parse document :" + xDoc.documentElement.childNodes[0].nodeValue); + } + return xDoc; + } else { + return this.load_xml_string(req.responseText); } }, load_xml_string : function(s) { @@ -298,17 +336,15 @@ QWeb2.Engine = (function() { var dp = new DOMParser(); var r = dp.parseFromString(s, "text/xml"); if (r.body && r.body.firstChild && r.body.firstChild.nodeName == 'parsererror') { - return this.tools.exception(r.body.innerText); + throw new Error("QWeb2: Could not parse document :" + r.body.innerText); } return r; } var xDoc; try { - // new ActiveXObject("Msxml2.DOMDocument.4.0"); xDoc = new ActiveXObject("MSXML2.DOMDocument"); } catch (e) { - return this.tools.exception( - "Could not find a DOM Parser: " + e.message); + throw new Error("Could not find a DOM Parser: " + e.message); } xDoc.async = false; xDoc.preserveWhiteSpace = true; @@ -325,7 +361,7 @@ QWeb2.Engine = (function() { try { return new ActiveXObject('MSXML2.XMLHTTP.3.0'); } catch (e) { - return null; + throw new Error("Could not get XHR"); } }, compile : function(node) {