[ADD] basics of on_write, split ListView.reload_record(index) in ListView.reload_record(record) and ListView.reload_record_at_index(index)

migrate most calls to reload_record(record)

bzr revid: xmo@openerp.com-20110823141300-zujxs2oaunqwhprt
This commit is contained in:
Xavier Morel 2011-08-23 16:13:00 +02:00
parent ea55848932
commit 37643a0909
2 changed files with 36 additions and 14 deletions

View File

@ -105,7 +105,7 @@ openerp.base.list_editable = function (openerp) {
} }
if (this.edition_index !== null) { if (this.edition_index !== null) {
this.reload_record(this.edition_index).then(function () { this.reload_record_at_index(this.edition_index).then(function () {
cancelled.resolve(); cancelled.resolve();
}); });
} else { } else {
@ -201,6 +201,21 @@ openerp.base.list_editable = function (openerp) {
}); });
}); });
}, },
handle_onwrite: function (record_id, index) {
var self = this;
var on_write_callback = self.view.fields_view.arch.attrs.on_write;
if (!on_write_callback) { return; }
this.dataset.call(on_write_callback, [record_id], function (ids) {
_(ids).each(function (id) {
var record = self.records.get(id);
if (!record) {
record = new openerp.base.list.Record({id: id});
self.records.add(record, {at: index});
}
self.reload_record(record);
});
});
},
/** /**
* Saves the current row, and triggers the edition of its following * Saves the current row, and triggers the edition of its following
* sibling if asked. * sibling if asked.
@ -215,6 +230,8 @@ openerp.base.list_editable = function (openerp) {
{at: self.options.editable === 'top' ? 0 : null}); {at: self.options.editable === 'top' ? 0 : null});
self.edition_index = self.dataset.index; self.edition_index = self.dataset.index;
} }
self.handle_onwrite(self.dataset.ids[self.dataset.index],
self.dataset.index);
self.cancel_pending_edition().then(function () { self.cancel_pending_edition().then(function () {
$(self).trigger('saved', [self.dataset]); $(self).trigger('saved', [self.dataset]);
if (!edit_next) { if (!edit_next) {

View File

@ -672,11 +672,10 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
var $target = $(e.currentTarget), var $target = $(e.currentTarget),
field = $target.closest('td').data('field'), field = $target.closest('td').data('field'),
$row = $target.closest('tr'), $row = $target.closest('tr'),
record_id = self.row_id($row), record_id = self.row_id($row);
index = self.row_position($row);
$(self).trigger('action', [field, record_id, function () { $(self).trigger('action', [field, record_id, function () {
return self.reload_record(index); return self.reload_record(self.records.get(record_id));
}]); }]);
}) })
.delegate('tr', 'click', function (e) { .delegate('tr', 'click', function (e) {
@ -757,25 +756,31 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
/** /**
* Reloads the record at index ``row_index`` in the list's rows. * Reloads the record at index ``row_index`` in the list's rows.
* *
* 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 {Number} record_index index of the record to reload
* @param {Boolean} fetch fetches the record from remote before reloading it * @returns {$.Deferred} promise to the finalization of the reloading
*/ */
reload_record: function (record_index) { reload_record_at_index: function (record_index) {
var r = this.records.at(record_index); var r = this.records.at(record_index);
return this.reload_record(r);
},
/**
* Reloads the provided record by re-reading its content from the server.
*
* @param {Record} record
* @returns {$.Deferred} promise to the finalization of the reloading
*/
reload_record: function (record) {
return this.dataset.read_ids( return this.dataset.read_ids(
[r.get('id')], [record.get('id')],
_.pluck(_(this.columns).filter(function (r) { _.pluck(_(this.columns).filter(function (r) {
return r.tag === 'field'; return r.tag === 'field';
}), 'name'), }), 'name'),
function (record) { function (records) {
_(record[0]).each(function (value, key) { _(records[0]).each(function (value, key) {
r.set(key, value, {silent: true}); record.set(key, value, {silent: true});
}); });
r.trigger('change', r); record.trigger('change', record);
} }
); );
}, },