I would like to know how to get the returned value from a Promise. I override the JS method for ListController so that the Action button menu list will be overridden according to some condition.
Here's my code:
odoo.define('custom_contacts.ListController', function (require) {
"use strict";
var core = require('web.core');
var ListController = require('web.ListController')
var rpc = require('web.rpc')
var Sidebar = require('web.Sidebar');
var _t = core._t;
ListController.include({
renderSidebar: function($node) {
var self = this;
var user_group = this._get_user_group()
var group_admin
Promise.resolve(user_group).then(function(value){
console.log(value)
group_admin = value
})
console.log(group_admin)
if (this.hasSidebar && group_admin) {
//if group_admin is true then show export, archive, unarchive menu in action button
var other = [{
label: _t("Export"),
callback: this._onExportData.bind(this)
}];
if (this.archiveEnabled) {
other.push({
label: _t("Archive"),
callback: function () {
Dialog.confirm(self, _t("Are you sure that you want to archive all the selected records?"), {
confirm_callback: self._onToggleArchiveState.bind(self, true),
});
}
});
other.push({
label: _t("Unarchive"),
callback: this._onToggleArchiveState.bind(this, false)
});
}
if (this.is_action_enabled('delete')) {
other.push({
label: _t('Delete'),
callback: this._onDeleteSelectedRecords.bind(this)
});
}
this.sidebar = new Sidebar(this, {
editable: this.is_action_enabled('edit'),
env: {
context: this.model.get(this.handle, {raw: true}).getContext(),
activeIds: this.getSelectedIds(),
model: this.modelName,
},
actions: _.extend(this.toolbarActions, {other: other}),
});
this.sidebar.appendTo($node);
this._toggleSidebar();
} else { // show export in action button only
var other = [{
label: _t("Export"),
callback: this._onExportData.bind(this)
}];
this.sidebar = new Sidebar(this, {
editable: this.is_action_enabled('edit'), env: {
context: this.model.get(this.handle, {raw: true}).getContext(),
activeIds: this.getSelectedIds(),
model: this.modelName,
},
actions: _.extend(this.toolbarActions, {other: other}),
});
this.sidebar.appendTo($node);
this._toggleSidebar(); }
},
_get_user_group: function(){
return rpc.query({
model: 'res.partner',
method: 'get_current_user_group',
args: [{}]
}).then(function(data) {
//data is either true or false
return data
})
},
});
return ListController; });
I always get undefined value in console.log(group_admin), however inside the .then() function value returned from the model is displayed correctly. Maybe I'm not doing it correctly. Any help will be greatly appreciated.. Thanks
Try to return the value in python method.
@Silverstar, yes i did already and the value is returned properly, this line of code:
var group_admin
Promise.resolve(user_group).then(function(value){
console.log(value) -> displays true or false (depends on the value)
group_admin = value
})
console.log(group_admin) -> displays undefined
Supposedly this line console.log(group_admin) should display true or false but it seems that the value being displayed here is from the declaration of the variable (var group_admin) first, than the new value that it is supposed to get from the .then() method...
Just an update.. I found the answer to this by doing:
var is_group_admin = false;
this.getSession().user_has_group('custom_contacts.group_admin').then(function(has_group) {
console.log(has_group + ' -> then()')
if(has_group) {
is_group_admin = true;
} else {
is_group_admin = false;
}
});
console.log(is_group_admin)
I used session to make this work..