From 348a44855d65ea780e5bbfcf536df49753cf6dd7 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 24 Mar 2017 08:39:35 +0100 Subject: [PATCH] [IMP] web: data_manager: make field description self-contained The fields are represented as an Object whose keys are the field names and values their description. By adding the name inside the description, this commit makes it fully self-contained. This commit also moves the processing of fields_views s.t. they are stored processed in the cache, instead of processing them each time they were requested. --- .../static/src/js/services/data_manager.js | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/addons/web/static/src/js/services/data_manager.js b/addons/web/static/src/js/services/data_manager.js index 5d5b71ece17..abcf10a2e6d 100644 --- a/addons/web/static/src/js/services/data_manager.js +++ b/addons/web/static/src/js/services/data_manager.js @@ -96,9 +96,7 @@ return core.Class.extend({ }).then(function (result) { // Postprocess fields_views and insert them into the fields_views cache result.fields_views = _.mapObject(result.fields_views, self._postprocess_fvg.bind(self)); - _.each(result.fields_views, function (fields_view) { - _.defaults(fields_view.fields, result.fields); - }); + self.processViews(result.fields_views, result.fields); _.each(views_descr, function (view_descr) { var toolbar = options.toolbar && view_descr[1] !== 'search'; var fv_key = self._gen_key(model, view_descr[0], view_descr[1], toolbar, context); @@ -114,15 +112,7 @@ return core.Class.extend({ }, this._invalidate.bind(this, this._cache.views, key)); } - return this._cache.views[key].then(function (views) { - return _.mapObject(views, function (view, viewType) { - return _.extend(view, self._processFieldsView({ - type: viewType, - arch: view.arch, - fields: view.fields, - })); - }); - }); + return this._cache.views[key]; }, /** @@ -188,6 +178,37 @@ return core.Class.extend({ }); }, + /** + * Processes fields and fields_views. For each field, writes its name inside + * the field description to make it self-contained. For each fields_view, + * completes its fields with the missing ones. + * + * @param {Object} fieldsViews object of fields_views (keys are view types) + * @param {Object} fields all the fields of the model + */ + processViews: function (fieldsViews, fields) { + var fieldName, fieldsView, viewType; + // write the field name inside the description for all fields + for (fieldName in fields) { + fields[fieldName].name = fieldName; + } + for (viewType in fieldsViews) { + fieldsView = fieldsViews[viewType]; + // write the field name inside the description for fields in view + for (fieldName in fieldsView.fields) { + fieldsView.fields[fieldName].name = fieldName; + } + // complete fields (in view) with missing ones + _.defaults(fieldsView.fields, fields); + // process the fields_view + _.extend(fieldsView, this._processFieldsView({ + type: viewType, + arch: fieldsView.arch, + fields: fieldsView.fields, + })); + } + }, + /** * Private function that postprocesses fields_view (mainly parses the arch attribute) */ @@ -205,7 +226,7 @@ return core.Class.extend({ id_field.type = 'id'; } - // Process inner views (one2manys) + // Process inner views (x2manys) _.each(fields_view.fields, function(field) { _.each(field.views || {}, function(view) { self._postprocess_fvg(view);