Last active
August 29, 2015 14:06
-
-
Save elchele/a5c0bddba074f5ba1054 to your computer and use it in GitHub Desktop.
Subpanel layout controller to hide empty subpanels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
/* Author: Angel Magaña -- [email protected] | |
* 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'); | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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