From 7c72a27d11051a94b19bc2c4a013d05b61fcbef9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 9 Mar 2011 12:57:46 +0100 Subject: [PATCH] [IMP] try adding some doc to qweb foreach bzr revid: xmo@openerp.com-20110309115746-4hkmq0oziwoi4jfs --- addons/base/static/qweb/qweb.js | 65 ++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/addons/base/static/qweb/qweb.js b/addons/base/static/qweb/qweb.js index fb4e8c67a5c..967b1976c85 100644 --- a/addons/base/static/qweb/qweb.js +++ b/addons/base/static/qweb/qweb.js @@ -209,17 +209,40 @@ var QWeb = { var r = this.eval_str(this.render_element(e, t_att, g_att, v), v); return t_att["js"] != "quiet" ? r : ""; }, - render_tag_foreach:function(e, t_att, g_att, v) { + /** + * Renders a foreach loop (@t-foreach). + * + * Adds the following elements to its context, where ${name} + * is specified via @t-as: + * * ${name} The current element itself + * * ${name}_value Same as ${name} + * * ${name}_index The 0-based index of the current element + * * ${name}_first Whether the current element is the first one + * * ${name}_parity odd|even (as strings) + * * ${name}_all The iterated collection itself + * + * If the collection being iterated is an array, also adds: + * * ${name}_last Whether the current element is the last one + * * All members of the current object + * + * If the collection being iterated is an object, the value is actually the object's key + * + * @param e ? + * @param t_att attributes of the element being t-foreach'd + * @param g_att ? + * @param old_context the context in which the foreach is evaluated + */ + render_tag_foreach:function(e, t_att, g_att, old_context) { var expr = t_att["foreach"]; - var enu = this.eval_object(expr, v); + var enu = this.eval_object(expr, old_context); var ru = []; if (enu) { var val = t_att['as'] || expr.replace(/[^a-zA-Z0-9]/g, '_'); - var d = {}; - for (var i in v) { - d[i] = v[i]; + var context = {}; + for (var i in old_context) { + context[i] = old_context[i]; } - d[val + "_all"] = enu; + context[val + "_all"] = enu; var val_value = val + "_value", val_index = val + "_index", val_first = val + "_first", @@ -227,32 +250,32 @@ var QWeb = { val_parity = val + "_parity"; var size = enu.length; if (size) { - d[val + "_size"] = size; + context[val + "_size"] = size; for (var j = 0; j < size; j++) { var cur = enu[j]; - d[val_value] = cur; - d[val_index] = j; - d[val_first] = j == 0; - d[val_last] = j + 1 == size; - d[val_parity] = (j % 2 == 1 ? 'odd' : 'even'); + context[val_value] = cur; + context[val_index] = j; + context[val_first] = j == 0; + context[val_last] = j + 1 == size; + context[val_parity] = (j % 2 == 1 ? 'odd' : 'even'); if (cur.constructor == Object) { for (var k in cur) { - d[k] = cur[k]; + context[k] = cur[k]; } } - d[val] = cur; - var r = this.render_element(e, t_att, g_att, d); + context[val] = cur; + var r = this.render_element(e, t_att, g_att, context); ru.push(r); } } else { var index = 0; for (cur in enu) { - d[val_value] = cur; - d[val_index] = index; - d[val_first] = index == 0; - d[val_parity] = (index % 2 == 1 ? 'odd' : 'even'); - d[val] = cur; - ru.push(this.render_element(e, t_att, g_att, d)); + context[val_value] = cur; + context[val_index] = index; + context[val_first] = index == 0; + context[val_parity] = (index % 2 == 1 ? 'odd' : 'even'); + context[val] = cur; + ru.push(this.render_element(e, t_att, g_att, context)); index += 1; } }