Refreshing Subpanels on the RecordView
Overview
How to refresh specific subpanels on the Record View.
Refreshing Subpanels
When Working with the Record View, it is sometimes necessary to force the refresh of a subpanel. The following example will demonstrate how to add buttons to force refresh a specific subpanel or all subpanels on the Accounts RecordView.
Adding the Button Metadata
For our example, we will first create a metadata extension file to append our custom refresh buttons to the Accounts RecordView action menu.
./custom/Extension/modules/Accounts/Ext/clients/base/views/record/refreshButtons.php
<?php
//module name
$module = 'Accounts';
//buttons to append
$addButtons = array(
array(
'type' => 'divider',
),
array (
'type' => 'rowaction',
'event' => 'button:refresh_specific_subpanel:click',
'name' => 'refresh_specific_subpanel',
'label' => 'LBL_REFRESH_SPECIFIC_SUBPANEL',
'acl_action' => 'view',
),
array (
'type' => 'rowaction',
'event' => 'button:refresh_all_subpanels:click',
'name' => 'refresh_all_subpanels',
'label' => 'LBL_REFRESH_ALL_SUBPANELS',
'acl_action' => 'view',
)
);
//if the buttons are missing in our base modules metadata, include core buttons
if (!isset($viewdefs[$module]['base']['view']['record']['buttons']))
{
require('clients/base/views/record/record.php');
$viewdefs[$module]['base']['view']['record']['buttons'] = $viewdefs['base']['view']['record']['buttons'];
unset($viewdefs['base']);
}
foreach($viewdefs[$module]['base']['view']['record']['buttons'] as $outerKey => $outerButton)
{
if (
isset($outerButton['type'])
&& $outerButton['type'] == 'actiondropdown'
&& isset($outerButton['name'])
&& $outerButton['name'] == 'main_dropdown'
&& isset($outerButton['buttons'])
)
{
/*
//removing buttons by name
foreach($viewdefs[$module]['base']['view']['record']['buttons'][$outerKey]['buttons'] as $innerKey => $innerButton)
{
if (
isset($innerButton['name'])
&& $innerButton['name'] == 'button_name'
)
{
unset($viewdefs[$module]['base']['view']['record']['buttons'][$outerKey]['buttons'][$innerKey]);
}
}
*/
//appending buttons
foreach ($addButtons as $addButton)
{
$viewdefs[$module]['base']['view']['record']['buttons'][$outerKey]['buttons'][]=$addButton;
}
}
}
Next, we will create our language labels for the buttons.
./custom/Extension/modules/Accounts/Ext/Language/en_us.refreshButtons.php
<?php
$mod_strings['LBL_REFRESH_SPECIFIC_SUBPANEL'] = 'Refresh Specific Subpanel';
$mod_strings['LBL_REFRESH_ALL_SUBPANELS'] = 'Refresh All Subpanels';
Our next step is to extend the Accounts controller file. This is where we will add our code to refresh the subpanels when the buttons are clicked.
./custom/modules/Accounts/clients/base/views/record/record.js
({
extendsFrom: 'RecordView',
initialize: function (options) {
this._super('initialize', [options]);
//add listeners for custom buttons
this.context.on('button:refresh_specific_subpanel:click', this.refresh_specific_subpanel, this);
this.context.on('button:refresh_all_subpanels:click', this.refresh_all_subpanels, this);
},
/**
* Refreshes a specific subpanel given a link
*/
refresh_specific_subpanel: function() {
var linkName = 'contacts';
var subpanelCollection = this.model.getRelatedCollection(linkName);
subpanelCollection.fetch({relate: true});
},
/**
* Refreshes all subpanels
*/
refresh_all_subpanels: function() {
_.each(this.model._relatedCollections, function(collection){
collection.fetch({relate: true});
});
}
})
Note: To refresh a specific subpanel, you will need to pass the linkname of the relationship to the this.model.getRelatedCollection()
method.
Finally, we will then need to navigate to Admin > Repair > Quick Repair and Rebuild. This will rebuild our extensions and make the refresh buttons available on our RecordView.