Load model access synchronously
It is needed if the list of model has changed since the login. issue6635 review37731002
This commit is contained in:
1 parent
6d82bf5a48
commit
4dd5606a08
2 files changed
+51
-41
No files matched your search
+19
-19
@@ -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();
|
||||
|
||||
+32
-22
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user