fixed issue by adding some parameters in to javascript call, basicaly we check if in admin url we have either store or website parameter and passing it to the script.
var a = window.location.pathname.split('/');
if(a.indexOf('website') !== -1){
a.splice(0, a.indexOf('website'));
params = {
'website': a[1]
};
} else {
if(a.indexOf('store') !== -1){
a.splice(0, a.indexOf('store'));
params = {
'store': a[1]
};
} else {
params = {
};
}
}
new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
loaderArea: false,
asynchronous: true,
parameters: params,
onSuccess: function(transport) {
var response = JSON.parse(transport.responseText);
}
});
also rewrited part of the save function, it seem the first method didn't save the config value directly but only after you click save manually.
\Magento\Framework\App\RequestInterface $request,
\Magento\Config\Model\Config\Factory $configFactory
...
$this->_configFactory = $configFactory;
$this->_request = $request;
...
$configData = [
'section' => 'path',
'website' => ($this->_request->getParam('website', 0) ? $this->_request->getParam('website', 0):null),
'store' => ($this->_request->getParam('store', 0) ? $this->_request->getParam('store', 0) : null),
'groups' => [
'to' => [
'fields' => [
'field' => [
'value' => $value,
],
],
],
],
];
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->_configFactory->create(['data' => $configData]);
$configModel->save();
now this saves value based on website / store view