From 5c0324f3214d95c3a2d34db7d6d145700501c762 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 26 Jul 2012 16:48:17 +0200 Subject: [PATCH] [IMP] Widget#setElement from init to reinstate $element always being there That makes precious little sense, but a bunch of widgets actually expect $element to exist before start() has been called. Also extract creating a node from the widget's node description into a private method, use that where possible. bzr revid: xmo@openerp.com-20120726144817-20caa8jkvg39ycho --- addons/web/static/src/js/corelib.js | 21 +++++++++++++++++---- addons/web/static/src/js/coresetup.js | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/js/corelib.js b/addons/web/static/src/js/corelib.js index b80543a34d8..3fafcab26fb 100644 --- a/addons/web/static/src/js/corelib.js +++ b/addons/web/static/src/js/corelib.js @@ -514,6 +514,7 @@ instance.web.WidgetMixin = _.extend({},instance.web.CallbackEnabledMixin, { */ init: function(parent) { instance.web.CallbackEnabledMixin.init.call(this); + this.setElement(this._make_descriptive()); this.setParent(parent); }, /** @@ -701,10 +702,7 @@ instance.web.Widget = instance.web.Class.extend(instance.web.WidgetMixin, { $el = $(_.str.trim(instance.web.qweb.render( this.template, {widget: this}))); } else { - var attrs = _.extend({}, this.attributes || {}); - if (this.id) { attrs.id = this.id; } - if (this.className) { attrs['class'] = this.className; } - $el = $(this.make(this.tagName, attrs)); + $el = this._make_descriptive(); } this.replaceElement($el); }, @@ -737,6 +735,8 @@ instance.web.Widget = instance.web.Class.extend(instance.web.WidgetMixin, { * @return {*} this */ setElement: function (element) { + // NB: completely useless, as WidgetMixin#init creates a $element + // always if (this.$element) { this.undelegateEvents(); } @@ -766,6 +766,19 @@ instance.web.Widget = instance.web.Class.extend(instance.web.WidgetMixin, { } return el; }, + /** + * Makes a potential root element from the declarative builder of the + * widget + * + * @return {jQuery} + * @private + */ + _make_descriptive: function () { + var attrs = _.extend({}, this.attributes || {}); + if (this.id) { attrs.id = this.id; } + if (this.className) { attrs['class'] = this.className; } + return $(this.make(this.tagName, attrs)); + }, delegateEvents: function () { var events = this.events; if (_.isEmpty(events)) { return; } diff --git a/addons/web/static/src/js/coresetup.js b/addons/web/static/src/js/coresetup.js index b86b0534e3b..181364ba533 100644 --- a/addons/web/static/src/js/coresetup.js +++ b/addons/web/static/src/js/coresetup.js @@ -21,7 +21,7 @@ instance.web.OldWidget = instance.web.Widget.extend({ this.element_id = this.element_id || _.uniqueId('widget-'); var tmp = document.getElementById(this.element_id); - this.setElement(tmp || this.make(this.tagName)); + this.setElement(tmp || this._make_descriptive()); }, renderElement: function() { var rendered = this.render();