[imp] added possibility to set value of a one2many using an on_change
bzr revid: nicolas.vanhoren@openerp.com-20110629153526-ewu12haop290o6ru
This commit is contained in:
parent
c2ceeb2c6f
commit
6df623519b
|
@ -527,6 +527,7 @@ openerp.base.BufferedDataSet = openerp.base.DataSetStatic.extend({
|
||||||
this.to_create = [];
|
this.to_create = [];
|
||||||
this.to_write = [];
|
this.to_write = [];
|
||||||
this.cache = [];
|
this.cache = [];
|
||||||
|
this.delete_all = false;
|
||||||
},
|
},
|
||||||
on_change: function() {},
|
on_change: function() {},
|
||||||
read_ids: function (ids, fields, callback) {
|
read_ids: function (ids, fields, callback) {
|
||||||
|
|
|
@ -1396,8 +1396,8 @@ var commands = {
|
||||||
return [commands.LINK_TO, id, false];
|
return [commands.LINK_TO, id, false];
|
||||||
},
|
},
|
||||||
// (5[, _[, _]])
|
// (5[, _[, _]])
|
||||||
FORGET_ALL: 5,
|
DELETE_ALL: 5,
|
||||||
'forget_all': function () {
|
'delete_all': function () {
|
||||||
return [5, false, false];
|
return [5, false, false];
|
||||||
},
|
},
|
||||||
// (6, _, ids) replaces all linked records with provided ids
|
// (6, _, ids) replaces all linked records with provided ids
|
||||||
|
@ -1469,6 +1469,7 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
set_value: function(value) {
|
set_value: function(value) {
|
||||||
value = value || [];
|
value = value || [];
|
||||||
var self = this;
|
var self = this;
|
||||||
|
this.dataset.reset_ids([]);
|
||||||
if(value.length >= 1 && value[0] instanceof Array) {
|
if(value.length >= 1 && value[0] instanceof Array) {
|
||||||
var ids = [];
|
var ids = [];
|
||||||
_.each(value, function(command) {
|
_.each(value, function(command) {
|
||||||
|
@ -1492,10 +1493,25 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
case commands.LINK_TO:
|
case commands.LINK_TO:
|
||||||
ids.push(command[1]);
|
ids.push(command[1]);
|
||||||
return;
|
return;
|
||||||
|
case commands.DELETE_ALL:
|
||||||
|
self.dataset.delete_all = true;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this._super(ids);
|
this._super(ids);
|
||||||
this.dataset.set_ids(ids);
|
this.dataset.set_ids(ids);
|
||||||
|
} else if (value.length >= 1 && typeof(value[0]) === "object") {
|
||||||
|
var ids = [];
|
||||||
|
this.dataset.delete_all = true;
|
||||||
|
_.each(value, function(command) {
|
||||||
|
var obj = {values: command};
|
||||||
|
obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
|
||||||
|
self.dataset.to_create.push(obj);
|
||||||
|
self.dataset.cache.push(_.clone(obj));
|
||||||
|
ids.push(obj.id);
|
||||||
|
});
|
||||||
|
this._super(ids);
|
||||||
|
this.dataset.set_ids(ids);
|
||||||
} else {
|
} else {
|
||||||
this._super(value);
|
this._super(value);
|
||||||
this.dataset.reset_ids(value);
|
this.dataset.reset_ids(value);
|
||||||
|
@ -1508,7 +1524,8 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this.dataset)
|
if (!this.dataset)
|
||||||
return [];
|
return [];
|
||||||
var val = _.map(this.dataset.ids, function(id) {
|
var val = this.dataset.delete_all ? [commands.delete_all()] : [];
|
||||||
|
val = val.concat(_.map(this.dataset.ids, function(id) {
|
||||||
var alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;});
|
var alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;});
|
||||||
if (alter_order) {
|
if (alter_order) {
|
||||||
return commands.create(alter_order.values);
|
return commands.create(alter_order.values);
|
||||||
|
@ -1518,7 +1535,7 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
return commands.update(alter_order.id, alter_order.values);
|
return commands.update(alter_order.id, alter_order.values);
|
||||||
}
|
}
|
||||||
return commands.link_to(id);
|
return commands.link_to(id);
|
||||||
});
|
}));
|
||||||
return val.concat(_.map(
|
return val.concat(_.map(
|
||||||
this.dataset.to_delete, function(x) {
|
this.dataset.to_delete, function(x) {
|
||||||
return commands['delete'](x.id);}));
|
return commands['delete'](x.id);}));
|
||||||
|
|
Loading…
Reference in New Issue