[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
This commit is contained in:
Xavier Morel
2012-07-26 16:48:17 +02:00
parent a9ccdfae03
commit 5c0324f321
2 changed files with 18 additions and 5 deletions
+17 -4
View File
@@ -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; }
+1 -1
View File
@@ -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();