summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/basic/basic_model.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2017-03-07 16:46:30 +0100
committerAaron Bohy <aab@odoo.com>2017-04-11 19:44:38 +0200
commit23f3d9524835d95473226aa566054344fe7cf349 (patch)
treeef7b584a0a8c2465886d7870af657162b0d8753f /addons/web/static/src/js/views/basic/basic_model.js
parentcac00211fe812939711a3aa77601e121a6abeed9 (diff)
[REF] web: factorise some code in BasicModel
Each data information coming from the server was serialized, and need to be deserialized in the proper format. This was done in 3 different places.
Diffstat (limited to 'addons/web/static/src/js/views/basic/basic_model.js')
-rw-r--r--addons/web/static/src/js/views/basic/basic_model.js119
1 files changed, 41 insertions, 78 deletions
diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js
index 8afd87aedfe..8239692711e 100644
--- a/addons/web/static/src/js/views/basic/basic_model.js
+++ b/addons/web/static/src/js/views/basic/basic_model.js
@@ -1170,18 +1170,7 @@ var BasicModel = AbstractModel.extend({
.then(function (records) {
_.each(records, function (record) {
list_records[record.id].data = record;
- _.each(record, function (val, fieldName) {
- var field = list.fields[fieldName];
- if (field.type === 'date') {
- // process data: convert into a moment instance
- record[fieldName] = fieldUtils.parse.date(val);
- } else if (field.type === 'datetime') {
- // process datetime: convert into a moment instance
- record[fieldName] = fieldUtils.parse.datetime(val);
- } else {
- record[fieldName] = val;
- }
- });
+ self._parseServerData(list.fieldNames, list.fields, record);
});
});
defs.push(def);
@@ -1318,33 +1307,7 @@ var BasicModel = AbstractModel.extend({
return self._postprocess(record);
});
}).then(function (record) {
- _.each(fieldNames, function (name) {
- var field = record.fields[name];
- var val = record.data[name];
- if (field.type === 'many2one') {
- // process many2one: split [id, nameget] and create corresponding record
- if (val !== false) {
- // the many2one value is of the form [id, display_name]
- var r = self._makeDataPoint({
- modelName: field.relation,
- data: {
- display_name: val[1],
- id: val[0],
- },
- });
- record.data[name] = r.id;
- } else {
- // no value for the many2one
- record.data[name] = false;
- }
- } else if (field.type === 'date') {
- // process data: convert into a moment instance
- record.data[name] = fieldUtils.parse.date(val);
- } else if (field.type === 'datetime') {
- // process datetime: convert into a moment instance
- record.data[name] = fieldUtils.parse.datetime(val);
- }
- });
+ self._parseServerData(fieldNames, record.fields, record.data);
return record;
});
},
@@ -1795,6 +1758,43 @@ var BasicModel = AbstractModel.extend({
return dataPoint;
},
/**
+ * parse the server values to javascript framwork
+ *
+ * @param {[string]} fieldNames
+ * @param {Object} fields
+ * @param {Object} record
+ */
+ _parseServerData: function (fieldNames, fields, record) {
+ var self = this;
+ _.each(fieldNames, function (fieldName) {
+ var field = fields[fieldName];
+ var val = record[fieldName];
+ if (field.type === 'many2one') {
+ // process many2one: split [id, nameget] and create corresponding record
+ if (val !== false) {
+ // the many2one value is of the form [id, display_name]
+ var r = self._makeDataPoint({
+ modelName: field.relation,
+ data: {
+ display_name: val[1],
+ id: val[0],
+ },
+ });
+ record[fieldName] = r.id;
+ } else {
+ // no value for the many2one
+ record[fieldName] = false;
+ }
+ } else if (field.type === 'date') {
+ // process data: convert into a moment instance
+ record[fieldName] = fieldUtils.parse.date(val);
+ } else if (field.type === 'datetime') {
+ // process datetime: convert into a moment instance
+ record[fieldName] = fieldUtils.parse.datetime(val);
+ }
+ });
+ },
+ /**
* Once a record is created and some data has been fetched, we need to do
* quite a lot of computations to determine what needs to be fetched. This
* method is doing that.
@@ -1996,25 +1996,7 @@ var BasicModel = AbstractModel.extend({
});
// add many2one records
- _.each(dataPoint.fieldNames, function (name) {
- var field = dataPoint.fields[name];
- if (field.type === 'many2one') {
- var r = self._makeDataPoint({
- modelName: field.relation,
- data: {
- id: dataPoint.data[name][0],
- display_name: dataPoint.data[name][1],
- },
- parentID: dataPoint.id,
- });
-
- dataPoint.data[name] = r.id;
- } else if (field.type === 'date') {
- dataPoint.data[name] = fieldUtils.parse.date(dataPoint.data[name]);
- } else if (field.type === 'datetime') {
- dataPoint.data[name] = fieldUtils.parse.datetime(dataPoint.data[name]);
- }
- });
+ self._parseServerData(list.fieldNames, dataPoint.fields, dataPoint.data);
list._cache[id] = dataPoint.id;
}
list.data.push(dataPoint.id);
@@ -2052,26 +2034,7 @@ var BasicModel = AbstractModel.extend({
});
// add many2one records
- _.each(dataPoint.fieldNames, function (name) {
- var field = dataPoint.fields[name];
- if (field.type === 'many2one') {
- var r = self._makeDataPoint({
- modelName: field.relation,
- data: {
- id: dataPoint.data[name][0],
- display_name: dataPoint.data[name][1],
- }
- });
-
- dataPoint.data[name] = r.id;
- }
- if (field.type === 'date') {
- dataPoint.data[name] = fieldUtils.parse.date(dataPoint.data[name]);
- }
- if (field.type === 'datetime') {
- dataPoint.data[name] = fieldUtils.parse.datetime(dataPoint.data[name]);
- }
- });
+ self._parseServerData(list.fieldNames, dataPoint.fields, dataPoint.data);
return dataPoint.id;
});
list.data = data;