summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/form_relational_widgets.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2015-08-19 17:35:44 +0200
committerChristophe Matthieu <chm@odoo.com>2015-08-27 15:01:09 +0200
commit0592305126f04c171ad38ebd2393526e01bf2cb9 (patch)
tree040914313cdd00360ab19dd595b0a66c02c2be37 /addons/web/static/src/js/views/form_relational_widgets.js
parenta102a9f3fcf778bd9cebe42a98cddece75c99369 (diff)
[REF] web: data.js: refactoring of BufferedDataSet (less code and speed up)
Diffstat (limited to 'addons/web/static/src/js/views/form_relational_widgets.js')
-rw-r--r--addons/web/static/src/js/views/form_relational_widgets.js97
1 files changed, 48 insertions, 49 deletions
diff --git a/addons/web/static/src/js/views/form_relational_widgets.js b/addons/web/static/src/js/views/form_relational_widgets.js
index a3e0286fb28..b5aaf38b5fa 100644
--- a/addons/web/static/src/js/views/form_relational_widgets.js
+++ b/addons/web/static/src/js/views/form_relational_widgets.js
@@ -442,19 +442,20 @@ var AbstractManyField = common.AbstractField.extend({
this.set('value', []);
this.starting_ids = [];
this.mutex = new utils.Mutex();
- this.has_not_committed_changes = false;
this.view.on("load_record", this, this._on_load_record);
this.dataset.on('dataset_changed', this, function() {
- self.has_not_committed_changes = true;
+ var options = _.clone(_.last(arguments));
+ if (!_.isObject(options) || _.isArray(options)) {
+ options = {};
+ }
// don't trigger changes if all commands are not resolved
// the editable lists change the dataset without call AbstractManyField methods
- if (self.mutex.def.state() === "resolved" && !self.internal_dataset_changed) {
- self.trigger("change:commands");
+ if (!self.internal_dataset_changed && !options.internal_dataset_changed) {
+ self.trigger("change:commands", options);
}
});
this.on("change:commands", this, function (options) {
- self.has_not_committed_changes = false;
- self._inhibit_on_change_flag = !!(options||{})._inhibit_on_change_flag;
+ self._inhibit_on_change_flag = !!options._inhibit_on_change_flag;
self.set({'value': self.dataset.ids.slice()});
self._inhibit_on_change_flag = false;
});
@@ -463,9 +464,10 @@ var AbstractManyField = common.AbstractField.extend({
_on_load_record: function (record) {
this.starting_ids = [];
// don't set starting_ids for the new record
- if (record.id && (!isNaN(record.id) || record.id.indexOf(this.dataset.virtual_id_prefix) === -1)) {
+ if (record.id && record[this.name] && (!isNaN(record.id) || record.id.indexOf(this.dataset.virtual_id_prefix) === -1)) {
this.starting_ids = record[this.name].slice();
}
+ this.trigger("load_record", record);
},
set_value: function(ids) {
@@ -574,6 +576,7 @@ var AbstractManyField = common.AbstractField.extend({
var dataset = this.dataset;
var res = true;
options = options || {};
+ var internal_options = _.extend({}, options, {'internal_dataset_changed': true});
_.each(command_list, function(command) {
self.mutex.exec(function() {
@@ -582,12 +585,12 @@ var AbstractManyField = common.AbstractField.extend({
case COMMANDS.CREATE:
var data = _.clone(command[2]);
delete data.id;
- return dataset.create(data, options).then(function (id) {
+ return dataset.create(data, internal_options).then(function (id) {
dataset.ids.push(id);
res = id;
});
case COMMANDS.UPDATE:
- return dataset.write(id, command[2], options).then(function () {
+ return dataset.write(id, command[2], internal_options).then(function () {
if (dataset.ids.indexOf(id) === -1) {
dataset.ids.push(id);
res = id;
@@ -599,14 +602,14 @@ var AbstractManyField = common.AbstractField.extend({
return dataset.unlink([id]);
case COMMANDS.LINK_TO:
if (dataset.ids.indexOf(id) === -1) {
- return dataset.add_ids([id], options);
+ return dataset.add_ids([id], internal_options);
}
return;
case COMMANDS.DELETE_ALL:
return dataset.reset_ids([], {keep_read_data: true});
case COMMANDS.REPLACE_WITH:
dataset.ids = [];
- return dataset.alter_ids(command[2], options);
+ return dataset.alter_ids(command[2], internal_options);
default:
throw new Error("send_commands to '"+self.name+"' receive a non command value." +
"\n" + JSON.stringify(command_list));
@@ -631,29 +634,27 @@ var AbstractManyField = common.AbstractField.extend({
replace_with_ids = [],
add_ids = [],
command_list = [],
- id, index, alter_order;
+ id, index, record;
- function format_many2one (values) {
- values = _.clone(values);
- for (var k in values) {
- if ((values[k] instanceof Array) && values[k].length === 2 && typeof values[k][0] === "number" && typeof values[k][1] === "string") {
- values[k] = values[k][0];
- }
- }
- return values;
- }
-
_.each(this.get('value'), function (id) {
index = starting_ids.indexOf(id);
if (index !== -1) {
starting_ids.splice(index, 1);
}
- if (alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;})) {
- command_list.push(COMMANDS.create(format_many2one(alter_order.values)));
- return;
- }
- if (alter_order = _.detect(self.dataset.to_write, function(x) {return x.id === id;})) {
- command_list.push(COMMANDS.update(alter_order.id, format_many2one(alter_order.values)));
+ var record = self.dataset.get_cache(id);
+ if (!_.isEmpty(record.changes)) {
+ var values = _.clone(record.changes);
+ // format many2one values
+ for (var k in values) {
+ if ((values[k] instanceof Array) && values[k].length === 2 && typeof values[k][0] === "number" && typeof values[k][1] === "string") {
+ values[k] = values[k][0];
+ }
+ }
+ if (record.to_create) {
+ command_list.push(COMMANDS.create(values));
+ } else {
+ command_list.push(COMMANDS.update(record.id, values));
+ }
return;
}
if (!is_one2many || self.dataset.delete_all) {
@@ -683,7 +684,7 @@ var AbstractManyField = common.AbstractField.extend({
},
is_valid: function () {
- return this.mutex.def.state() === "resolved" && !this.has_not_committed_changes && this._super();
+ return this.mutex.def.state() === "resolved" && this._super();
},
is_false: function() {
@@ -853,9 +854,7 @@ var FieldX2Many = AbstractManyField.extend({
if (view && view.type === "list" && view.controller.__focus) {
var def = $.Deferred();
view.controller._on_blur_one2many(true).always(function () {
- setTimeout(function () {def.resolve();},0);
- }, function () {
- setTimeout(function () {def.reject();},0);
+ def.resolve();
});
return def;
}
@@ -887,17 +886,7 @@ var X2ManyDataSet = data.BufferedDataSet.extend({
self.context.add(context);
});
return this.context;
- },
- create: function(data, options) {
- var self = this;
- var def = this._super(data, options);
- def.then(function (id) {
- setTimeout(function () {
- self.trigger("dataset_changed", id, data, options);
- },0);
- });
- return def;
- },
+ }
});
var X2ManyViewManager = ViewManager.extend({
@@ -1046,6 +1035,7 @@ var X2ManyList = ListView.List.extend({
var One2ManyListView = X2ManyListView.extend({
_template: 'One2Many.listview',
init: function (parent, dataset, view_id, options) {
+ var self = this;
this._super(parent, dataset, view_id, _.extend(options || {}, {
GroupsType: One2ManyGroups,
ListType: X2ManyList
@@ -1065,6 +1055,13 @@ var One2ManyListView = X2ManyListView.extend({
}
};
$(document).on('mousedown', this._mousedown_blur_line);
+
+ this.dataset.on('dataset_changed', this, function () {
+ self._dataset_changed = true;
+ });
+ this.dataset.x2m.on('load_record', this, function () {
+ self._dataset_changed = false;
+ });
},
do_add_record: function () {
if (this.editable()) {
@@ -1130,10 +1127,10 @@ var One2ManyListView = X2ManyListView.extend({
return $.when();
}).done(function () {
var ds = self.x2m.dataset;
- var cached_records = _.any([ds.to_create, ds.to_delete, ds.to_write], function(value) {
- return value.length;
+ var changed_records = _.find(ds.cache, function(record) {
+ return record.to_create || record.to_delete || !_.isEmpty(record.changes);
});
- if (!self.x2m.options.reload_on_button && !cached_records) {
+ if (!self.x2m.options.reload_on_button && !changed_records) {
self.handle_button(name, id, callback);
}else {
self.handle_button(name, id, function(){
@@ -1160,6 +1157,7 @@ var One2ManyListView = X2ManyListView.extend({
},
_on_focus_one2many: function () {
this.dataset.x2m.internal_dataset_changed = true;
+ this._dataset_changed = false;
this.__focus = true;
},
_on_blur_one2many: function (force) {
@@ -1180,11 +1178,12 @@ var One2ManyListView = X2ManyListView.extend({
def = this.cancel_edition();
}
- def.then(function () {
- if (self.dataset.x2m.has_not_committed_changes) {
+ def.then(function () {
+ if (self._dataset_changed) {
self.dataset.trigger('dataset_changed');
}
- });
+ });
+
return def;
},
_after_edit: function () {