From 885fa0ded0b7417be2fe2aad28a51f2a53ef92a8 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 22 Aug 2011 11:04:58 +0200 Subject: [PATCH] [ADD] handling of adding new records to an existing collection, silent option on a bunch of record events bzr revid: xmo@openerp.com-20110822090458-3wdroe4cxyh30k91 --- addons/base/static/src/js/formats.js | 3 ++ addons/base/static/src/js/list-editable.js | 2 ++ addons/base/static/src/js/list.js | 40 ++++++++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/addons/base/static/src/js/formats.js b/addons/base/static/src/js/formats.js index 074cabe5eee..abb05ec9dc4 100644 --- a/addons/base/static/src/js/formats.js +++ b/addons/base/static/src/js/formats.js @@ -201,6 +201,9 @@ openerp.base.format_cell = function (row_data, column, value_if_empty) { ].join('') } + if (!row_data[column.id]) { + return value_if_empty === undefined ? '' : value_if_empty; + } return openerp.base.format_value( row_data[column.id].value, column, value_if_empty); } diff --git a/addons/base/static/src/js/list-editable.js b/addons/base/static/src/js/list-editable.js index c89227a05cb..744f3c9a09b 100644 --- a/addons/base/static/src/js/list-editable.js +++ b/addons/base/static/src/js/list-editable.js @@ -209,6 +209,8 @@ openerp.base.list.editable = function (openerp) { var self = this; this.edition_form.do_save(function (result) { if (result.created && !self.edition_index) { + self.records.add({id: result.result}, + {at: self.options.editable === 'top' ? 0 : null}); self.edition_index = self.dataset.index; } self.cancel_pending_edition().then(function () { diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js index 642a0743e9a..f35d5318cdc 100644 --- a/addons/base/static/src/js/list.js +++ b/addons/base/static/src/js/list.js @@ -642,6 +642,20 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base. 'change': function (event, record) { var $row = self.$current.find('[data-id=' + record.get('id') + ']'); $row.replaceWith(self.render_record($row.data('index'))); + }, + 'add': function (ev, records, record, index) { + var $row = $('').attr({ + 'data-id': record.get('id'), + 'data-index': index + }); + if (index === 0) { + $row.prependTo(self.$current); + } else { + $row.insertAfter(self.$current.children().eq(index)); + } + $row.nextAll().each(function (row) { + $(row).data('index', ++index); + }); } }; _(this.record_callbacks).each(function (callback, event) { @@ -767,8 +781,9 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base. }), 'name'), function (record) { _(record[0]).each(function (value, key) { - r.set(key, value); + r.set(key, value, {silent: true}); }); + r.trigger('change', r); } ); }, @@ -1033,7 +1048,7 @@ openerp.base.ListView.Groups = openerp.base.Class.extend( /** @lends openerp.bas .attr('disabled', page === pages - 1); } - self.records.add(records); + self.records.add(records, {silent: true}); list.render(); d.resolve(list); }); @@ -1211,16 +1226,21 @@ var Record = openerp.base.Class.extend(/** @lends Record# */{ /** * @param key * @param value + * @param {Object} [options] + * @param {Boolean} [options.silent=false] * @returns {Record} */ - set: function (key, value) { + set: function (key, value, options) { + options = options || {}; var old_value = this.attributes[key]; if (old_value === value) { return this; } this.attributes[key] = value; - this.trigger('change:' + key, this, value, old_value); - this.trigger('change', this, key, value, old_value); + if (!options.silent) { + this.trigger('change:' + key, this, value, old_value); + this.trigger('change', this, key, value, old_value); + } return this; }, /** @@ -1279,6 +1299,7 @@ var Collection = openerp.base.Class.extend(/** @lends Collection# */{ * @param {Object|Array} record * @param {Object} [options] * @param {Number} [options.at] + * @param {Boolean} [options.silent=false] * @returns this */ add: function (record, options) { @@ -1291,8 +1312,15 @@ var Collection = openerp.base.Class.extend(/** @lends Collection# */{ this._byId[instance.get('id')] = instance; if (options.at === undefined) { this.records.push(instance); + if (!options.silent) { + this.trigger('add', this, instance, this.records.length-1); + } } else { - this.records.splice(options.at + i, 0, instance); + var insertion_index = options.at + i; + this.records.splice(insertion_index, 0, instance); + if (!options.silent) { + this.trigger('add', this, instance, insertion_index); + } } this.length++; }