[FIX] change event handling for 'new' records (records w/o an id yet)

bzr revid: xmo@openerp.com-20120702111026-twmh5gbg4i1fd4kj
This commit is contained in:
Xavier Morel 2012-07-02 13:10:26 +02:00
parent c262b2d1ae
commit d6a9d86e09
2 changed files with 18 additions and 8 deletions

View File

@ -905,27 +905,36 @@ instance.web.ListView.List = instance.web.Class.extend( /** @lends instance.web.
this.record_callbacks = { this.record_callbacks = {
'remove': function (event, record) { 'remove': function (event, record) {
var $row = self.$current.find( var $row = self.$current.children(
'[data-id=' + record.get('id') + ']'); '[data-id=' + record.get('id') + ']');
var index = $row.data('index'); var index = $row.data('index');
$row.remove(); $row.remove();
self.refresh_zebra(index); self.refresh_zebra(index);
}, },
'reset': function () { return self.on_records_reset(); }, 'reset': function () { return self.on_records_reset(); },
'change': function (event, record) { 'change': function (event, record, attribute, value, old_value) {
var $row = self.$current.find('[data-id=' + record.get('id') + ']'); var $row;
if (attribute === 'id') {
if (old_value) {
throw new Error("Setting 'id' attribute on existing record "
+ JSON.stringify(record.attributes));
}
// Set id on new record
$row = self.$current.children('tr:not([data-id])');
} else {
$row = self.$current.children(
'[data-id=' + record.get('id') + ']');
}
$row.replaceWith(self.render_record(record)); $row.replaceWith(self.render_record(record));
}, },
'add': function (ev, records, record, index) { 'add': function (ev, records, record, index) {
var $new_row = $('<tr>').attr({ var $new_row = $(self.render_record(record));
'data-id': record.get('id')
});
if (index === 0) { if (index === 0) {
$new_row.prependTo(self.$current); $new_row.prependTo(self.$current);
} else { } else {
var previous_record = records.at(index-1), var previous_record = records.at(index-1),
$previous_sibling = self.$current.find( $previous_sibling = self.$current.children(
'[data-id=' + previous_record.get('id') + ']'); '[data-id=' + previous_record.get('id') + ']');
$new_row.insertAfter($previous_sibling); $new_row.insertAfter($previous_sibling);
} }

View File

@ -170,8 +170,9 @@ openerp.web.list_editable = function (instance) {
record = self.records.find(function (r) { record = self.records.find(function (r) {
return !r.get('id'); return !r.get('id');
}); });
record.set('id', attrs.id, {silent: true}); record.set('id', attrs.id);
} }
self.reload_record(record); self.reload_record(record);
}); });
}, },