Browse Source

Add support of many2one

Inc vers number
pull/918/head
hparfr 5 years ago
parent
commit
865ba7b5e4
  1. 4
      web_cache_name_get/README.rst
  2. 2
      web_cache_name_get/__manifest__.py
  3. 79
      web_cache_name_get/static/src/js/view_list.js

4
web_cache_name_get/README.rst

@ -19,7 +19,7 @@ For instance, if you have 40 sale order lines on a sale. All these lines have th
- Without this module : your browser makes about 40 time the same name_get() request.
- With this modules: your browser makes 1 request and reuse the result for all the lines.
It works on many2many on list views.
It works on many2many and many2one on list views.
The cache is cleaned when you navigate to other objects.
@ -35,6 +35,8 @@ Known issues / Roadmap
======================
* The cache is not very aggressive, if put more globally it may catch more useless requests.
* some requests can be done twice in many2one, because of 'this' been erased in
other parts of the core code.
Credits

2
web_cache_name_get/__manifest__.py

@ -4,7 +4,7 @@
{
"name": "Cache name_get",
"version": "10.0.1.0.0",
"version": "10.0.1.0.1",
"website": "https://github.com/OCA/web",
"author": "Akretion, "
"Odoo Community Association (OCA)",

79
web_cache_name_get/static/src/js/view_list.js

@ -3,21 +3,66 @@ odoo.define('web_cache_name_get.view_list', function(require) {
var listView = require('web.ListView');
var Model = require('web.DataModel');
var data = require('web.data');
var core = require('web.core');
var _t = core._t;
listView.List.include({
web_cache_name_get: function(key, callback) {
//will return a promise with the result of
//the name_get
//it will cache the request in order to save brandwith
//and useless requests
var prom = null;
var key = JSON.stringify(key);
//init cache if not already done
this.name_get_cache = this.name_get_cache || {};
if (this.name_get_cache[key]) {
//cache hit
prom = this.name_get_cache[key];
}
if (!prom) {
//cache miss
prom = callback();
this.name_get_cache[key] = prom
}
return prom;
},
/*
cache name_get of many2many in order to reduce
cache name_get of many2many and many2one in order to reduce
useless requests
first part is a copy paste of web/static/src/js/views/list_view.js
some parts are a copy paste of web/static/src/js/views/list_view.js
because we have hook into it and can't do much better than that.
*/
render_cell: function (record, column) {
var value;
if (column.type !== 'many2many') {
return this._super(record, column);
} else {
var self = this;
if (column.type === 'many2one') {
value = record.get(column.id);
// m2o values are usually name_get formatted, [Number, String]
// pairs, but in some cases only the id is provided. In these
// cases, we need to perform a name_get call to fetch the actual
// displayable value
if (typeof value === 'number' || value instanceof Number) {
// fetch the name, set it on the record (in the right field)
// and let the various registered events handle refreshing the
// row
// our logic starts here
prom = this.web_cache_name_get(
[this.view.model, this.view.name, column.relation, value],
function () {
return new data.DataSet(self.view, column.relation)
.name_get([value]);
}
);
prom.done(function (names) {
if (!names.length) { return; }
record.set(column.id, names[0]);
});
}
} else if (column.type === 'many2many') {
value = record.get(column.id);
// non-resolved (string) m2m values are arrays
if (value instanceof Array && !_.isEmpty(value)
@ -37,21 +82,15 @@ odoo.define('web_cache_name_get.view_list', function(require) {
// 2. an array of ids
ids = value;
}
// the logic starts here
// create a cache if not already done
this.name_get_cache = this.name_get_cache || {};
var prom = null;
var key = JSON.stringify([column.relation, ids, this.dataset.get_context()]);
// our logic starts here
var prom = this.web_cache_name_get(
[column.relation, ids, this.dataset.get_context()],
function () {
return new Model(column.relation)
.call('name_get', [ids, self.dataset.get_context()])
}
);
if (this.name_get_cache[key]) {
prom = this.name_get_cache[key];
}
if (!prom) {
prom = new Model(column.relation)
.call('name_get', [ids, this.dataset.get_context()]);
this.name_get_cache[key] = prom;
}
// put placeholder at first to limit races conditions
// because of the cached answers
// promise resolution can happen before placeholder set
@ -66,6 +105,8 @@ odoo.define('web_cache_name_get.view_list', function(require) {
record.set(column.id, ids);
});
}
} else {
return this._super(record, column);
}
return column.format(record.toForm().data, {
model: this.dataset.model,

Loading…
Cancel
Save