[IMP] web: remove rpc call on SwitchCompany widget loading
By setting the information returned by read_companies on the session information, we are able to remove the rpc call and the willStart method definition Also: Set a debounce on SwitchCompany widget to avoid multiple RPC
This commit is contained in:
@@ -710,6 +710,8 @@ class Session(http.Controller):
|
||||
|
||||
def session_info(self):
|
||||
request.session.ensure_valid()
|
||||
user = request.env.user
|
||||
display_switch_company_menu = user.has_group('base.group_multi_company') and len(user.company_ids) > 1
|
||||
return {
|
||||
"session_id": request.session_id,
|
||||
"uid": request.session.uid,
|
||||
@@ -718,6 +720,7 @@ class Session(http.Controller):
|
||||
"username": request.session.login,
|
||||
"company_id": request.env.user.company_id.id if request.session.uid else None,
|
||||
"partner_id": request.env.user.partner_id.id if request.session.uid and request.env.user.partner_id else None,
|
||||
"user_companies": {'current_company': (user.company_id.id, user.company_id.name), 'allowed_companies': [(comp.id, comp.name) for comp in user.company_ids]} if display_switch_company_menu else False,
|
||||
}
|
||||
|
||||
@http.route('/web/session/get_session_info', type='json', auth="none")
|
||||
|
||||
@@ -8,39 +8,28 @@ odoo.define('web.SwitchCompanyMenu', function(require) {
|
||||
|
||||
var SwitchCompanyMenu = Widget.extend({
|
||||
template: 'SwitchCompanyMenu',
|
||||
willStart: function(){
|
||||
var self = this;
|
||||
var def = $.Deferred();
|
||||
new Model("res.users").call("read_companies").then(function(res) {
|
||||
if (res){
|
||||
self.all_allowed_companies = res.all_allowed_companies;
|
||||
self.current_company = res.current_company;
|
||||
def.resolve();
|
||||
} else {
|
||||
def.reject();
|
||||
}
|
||||
}).fail(function(){
|
||||
def.reject();
|
||||
});
|
||||
return $.when(this._super(), def);
|
||||
willStart: function() {
|
||||
if (!session.user_companies) {
|
||||
return $.Deferred().reject();
|
||||
}
|
||||
return this._super();
|
||||
},
|
||||
start: function() {
|
||||
var self = this;
|
||||
|
||||
this.$el.on('click', '.dropdown-menu li a[data-menu]', function(ev) {
|
||||
this.$el.on('click', '.dropdown-menu li a[data-menu]', _.debounce(function(ev) {
|
||||
ev.preventDefault();
|
||||
var company_id = $(ev.currentTarget).data('company-id');
|
||||
new Model('res.users').call('write', [[session.uid], {'company_id': company_id}]).then(function() {
|
||||
location.reload();
|
||||
});
|
||||
});
|
||||
}, 1500, true));
|
||||
|
||||
self.$('.oe_topbar_name').text(self.current_company[1]);
|
||||
self.$('.oe_topbar_name').text(session.user_companies.current_company[1]);
|
||||
|
||||
var companies_list = '';
|
||||
_.each(self.all_allowed_companies, function(company) {
|
||||
_.each(session.user_companies.allowed_companies, function(company) {
|
||||
var a = '';
|
||||
if(company[0] == self.current_company[0]) {
|
||||
if(company[0] == session.user_companies.current_company[0]) {
|
||||
a = '<i class="fa fa-check o_current_company"></i>';
|
||||
} else {
|
||||
a = '<span class="o_company"/>';
|
||||
|
||||
@@ -308,11 +308,6 @@ class res_users(osv.osv):
|
||||
|
||||
return result
|
||||
|
||||
def read_companies(self, cr, uid, context=None):
|
||||
user = self.browse(cr, SUPERUSER_ID, uid, context=context)
|
||||
display_switch_company_menu = self.has_group(cr, uid, 'base.group_multi_company') and len(user.company_ids) > 1
|
||||
return {'current_company': (user.company_id.id, user.company_id.name), 'all_allowed_companies': [(comp.id, comp.name) for comp in user.company_ids]} if display_switch_company_menu else False
|
||||
|
||||
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
|
||||
if uid != SUPERUSER_ID:
|
||||
groupby_fields = set([groupby] if isinstance(groupby, basestring) else groupby)
|
||||
|
||||
Reference in New Issue
Block a user