Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active August 29, 2015 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/a5c0bddba074f5ba1054 to your computer and use it in GitHub Desktop.
Save elchele/a5c0bddba074f5ba1054 to your computer and use it in GitHub Desktop.
Subpanel layout controller to hide empty subpanels
({
/* Author: Angel Magaña -- cheleguanaco@cheleguanaco.com
* File: ./custom/clients/base/layouts/subpanels/subpanels.js
*
* Extended subpanels layout controller for hiding
* subpanels without data upon parent record load (Sugar 7.2+)
*
* This customization applies to all modules
*/
extendsFrom: 'SubpanelsLayout',
showSubpanel: function(linkName) {
this._super('showSubpanel', [linkName]);
var currentModel = app.controller.context.get('model');
/* currentModel.module returns the current parent module name
* which in turn could be used to selectively apply customization
* to specific modules only, rather than system wide
*/
/* Begin logic to check subpanel data and hide accordingly */
_.each(this._components, function(component) {
//Get link name (relationship) and load related records
var link = component.context.get('link');
var linkedData = currentModel.getRelatedCollection(link);
var linkLength = -1;
//Check how many related records were loaded for each link
linkedData.once('reset', function(collection){
linkLength = collection.length;
//Hide subpanels without any data
if (linkLength === 0)
{
component.remove();
}
});
});
},
_dispose: function(){
this._super('_dispose');
},
})
@maranemil
Copy link

Thanks for your post, you saved my day. :)

Using getRelatedCollection() I was able to trigger when the last Subpanel is loaded, kind of onReady for Subpanels Rest API requests, counting collections.

Example:
https://gist.github.com/maranemil/c088fbfe57ccbb2c33ea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment