From 7ba2688e39c9c929a1306353764c38f7b0c343b0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 3 Jun 2011 16:37:23 +0200 Subject: [PATCH] [IMP] reload of rows after action: only reload the row, not the whole tree Also improve DataSet slightly: have read_index and read_ids return their RPC promises bzr revid: xmo@openerp.com-20110603143723-go25vripams72bqb --- addons/base/static/src/js/data.js | 6 +-- addons/base/static/src/js/list-editable.js | 24 ++++------ addons/base/static/src/js/list.js | 52 +++++++++++++--------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/addons/base/static/src/js/data.js b/addons/base/static/src/js/data.js index 7582e99b662..b7739a3da27 100644 --- a/addons/base/static/src/js/data.js +++ b/addons/base/static/src/js/data.js @@ -259,7 +259,7 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base. */ read_ids: function (ids, fields, callback) { var self = this; - this.rpc('/base/dataset/get', { + return this.rpc('/base/dataset/get', { model: this.model, ids: ids, fields: fields @@ -279,10 +279,10 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base. */ read_index: function (fields, callback) { if (_.isEmpty(this.ids)) { - callback([]); + return $.Deferred().reject().promise(); } else { fields = fields || false; - this.read_ids([this.ids[this.index]], fields, function(records) { + return this.read_ids([this.ids[this.index]], fields, function(records) { callback(records[0]); }); } diff --git a/addons/base/static/src/js/list-editable.js b/addons/base/static/src/js/list-editable.js index ee2af7d258b..d51fa695de9 100644 --- a/addons/base/static/src/js/list-editable.js +++ b/addons/base/static/src/js/list-editable.js @@ -99,22 +99,16 @@ openerp.base.list.editable = function (openerp) { save_row: function (row_num, edit_next) { var self = this; this.edition_form.do_save(function () { - self.dataset.read_index( - _.filter(_.pluck(self.columns, 'name'), _.identity), - function (record) { - var form_record = self.transform_record(record); - self.rows.splice(row_num, 1, form_record); - self.reload_record(row_num); - self.edition_form.stop(); - delete self.edition_form; - if (edit_next && self.rows.length > row_num + 1) { - self.dataset.index++; - self.row_clicked({ - currentTarget: self.$current.children().eq(row_num + 1) - }, row_num + 1); - } + self.reload_record(row_num, true).then(function () { + self.edition_form.stop(); + delete self.edition_form; + if (edit_next && self.rows.length > row_num + 1) { + self.dataset.index++; + self.row_clicked({ + currentTarget: self.$current.children().eq(row_num + 1) + }, row_num + 1); } - ); + }); }); }, /** diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js index c627330fd70..8d19635968a 100644 --- a/addons/base/static/src/js/list.js +++ b/addons/base/static/src/js/list.js @@ -545,9 +545,13 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List e.stopPropagation(); var $target = $(e.currentTarget), field = $target.closest('td').data('field'), - record_id = self.row_id($target.closest('tr')); + $row = $target.closest('tr'), + record_id = self.row_id($row), + index = self.row_position($row); - $(self).trigger('action', [field, record_id]); + $(self).trigger('action', [field, record_id, function () { + self.reload_record(index, true); + }]); }) .delegate('tr', 'click', function (e) { e.stopPropagation(); @@ -671,14 +675,32 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List /** * Reloads the record at index ``row_index`` in the list's rows. * - * This does not re-fetch the record, it only re-renders it, replacing the - * current rendering. + * By default, simply re-renders the record. If the ``fetch`` parameter is + * provided and ``true``, will first fetch the record anew. * * @param {Number} record_index index of the record to reload + * @param {Boolean} fetch fetches the record from remote before reloading it */ - reload_record: function (record_index) { - this.$current.children().eq(record_index) - .replaceWith(this.render_record(record_index)); + reload_record: function (record_index, fetch) { + var self = this; + var read_p = null; + if (fetch) { + // save index to restore it later, if already set + var old_index = this.dataset.index; + this.dataset.index = record_index; + read_p = this.dataset.read_index( + _.filter(_.pluck(this.columns, 'name'), _.identity), + function (record) { + var form_record = self.transform_record(record); + self.rows.splice(record_index, 1, form_record); + self.dataset.index = old_index; + } + ) + } + + return $.when(read_p).then(function () { + self.$current.children().eq(record_index) + .replaceWith(self.render_record(record_index)); }) }, /** * Renders a list record to HTML @@ -848,21 +870,7 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr // can have selections spanning multiple links var selection = self.get_selection(); $this.trigger(e, [selection.ids, selection.records]); - }).bind('action', function (e, name, id, callback) { - if (!callback) { - callback = function () { - var $prev = child.$current.prev(); - if (!$prev.is('tbody')) { - // ungrouped - $(self.elements[0]).replaceWith(self.render()); - } else { - // ghetto reload child (and its siblings) - $prev.children().last().click(); - } - }; - } - $this.trigger(e, [name, id, callback]); - }).bind('deleted row_link', function (e) { + }).bind('action deleted row_link', function (e) { // additional positional parameters are provided to trigger as an // Array, following the event type or event object, but are // provided to the .bind event handler as *args.