From 4dd5606a08ab6fcbb99df25dd9ff514160d2f629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 13 Oct 2017 23:36:18 +0200 Subject: [PATCH] Load model access synchronously It is needed if the list of model has changed since the login. issue6635 review37731002 --- src/common.js | 38 ++++++++++++++++++------------------ src/rpc.js | 54 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/common.js b/src/common.js index 3f79cf84c7..59da75ddac 100644 --- a/src/common.js +++ b/src/common.js @@ -414,6 +414,7 @@ Sao.common.ModelAccess = Sao.class_(Object, { init: function() { this.batchnum = 100; + this._models = []; this._access = {}; }, load_models: function(refresh) { @@ -421,29 +422,28 @@ if (!refresh) { this._access = {}; } - Sao.rpc({ + this._models = Sao.rpc({ 'method': 'model.ir.model.list_models', 'params': [{}] - }, Sao.Session.current_session).then(function(models) { - var deferreds = []; - var update_access = function(access) { - this._access = jQuery.extend(this._access, access); - }; - for (var i = 0; i < models.length; i += this.batchnum) { - var to_load = models.slice(i, i + this.batchnum); - deferreds.push(Sao.rpc({ - 'method': 'model.ir.model.access.get_access', - 'params': [to_load, {}] - }, Sao.Session.current_session) - .then(update_access.bind(this))); - } - jQuery.when.apply(jQuery, deferreds).then( - prm.resolve, prm.reject); - }.bind(this)); - return prm; + }, Sao.Session.current_session, false); }, get: function(model) { - return this._access[model] || {}; + if (this._access[model] !== undefined) { + return this._access[model]; + } + var idx = this._models.indexOf(model); + if (idx < 0) { + this.load_models(false); + } + var to_load = this._models.slice( + Math.max(0, idx - Math.floor(this.batchnum / 2)), + idx + Math.floor(this.batchnum / 2)); + var access = Sao.rpc({ + 'method': 'model.ir.model.access.get_access', + 'params': [to_load, {}] + }, Sao.Session.current_session, false); + this._access = jQuery.extend(this._access, access); + return this._access[model]; } }); Sao.common.MODELACCESS = new Sao.common.ModelAccess(); diff --git a/src/rpc.js b/src/rpc.js index 0943a52ed7..5d67d95261 100644 --- a/src/rpc.js +++ b/src/rpc.js @@ -3,32 +3,19 @@ (function() { 'use strict'; - Sao.rpc = function(args, session) { - var dfd = jQuery.Deferred(); + Sao.rpc = function(args, session, async) { + var dfd = jQuery.Deferred(), + result; if (!session) { session = new Sao.Session(); } + if (async === undefined) { + async = true; + } var params = jQuery.extend([], args.params); params.push(jQuery.extend({}, session.context, params.pop())); var timeoutID = Sao.common.processing.show(); - var ajax_prm = jQuery.ajax({ - 'headers': { - 'Authorization': 'Session ' + session.get_auth() - }, - 'contentType': 'application/json', - 'data': JSON.stringify(Sao.rpc.prepareObject({ - 'id': Sao.rpc.id++, - 'method': args.method, - 'params': params - })), - 'dataType': 'json', - 'url': '/' + (session.database || '') + '/', - 'type': 'post', - 'complete': [function() { - Sao.common.processing.hide(timeoutID); - }] - }); var ajax_success = function(data) { if (data === null) { @@ -90,6 +77,7 @@ dfd.reject(); } else { dfd.resolve(data.result); + result = data.result; } }; @@ -105,10 +93,32 @@ dfd.reject(); } }; - ajax_prm.success(ajax_success); - ajax_prm.error(ajax_error); - return dfd.promise(); + jQuery.ajax({ + 'async': async, + 'headers': { + 'Authorization': 'Session ' + session.get_auth() + }, + 'contentType': 'application/json', + 'data': JSON.stringify(Sao.rpc.prepareObject({ + 'id': Sao.rpc.id++, + 'method': args.method, + 'params': params + })), + 'dataType': 'json', + 'url': '/' + (session.database || '') + '/', + 'type': 'post', + 'complete': [function() { + Sao.common.processing.hide(timeoutID); + }], + 'success': ajax_success, + 'error': ajax_error, + }); + if (async) { + return dfd.promise(); + } else { + return result; + } }; Sao.rpc.id = 0;