I have the following javascript code snippet, while I understand what's it doing, I fail to understand the whole object creation "Sample". Is this some best practice to write JS code?
var Sample = window.Sample || {}; (function () { console.log('file loaded'); // Show/hide tabs based on Relationship Type this.showHideTabs = function (executionContext) { console.log('method called'); var formContext = executionContext.getFormContext(); var relType = ''; if (formContext.getAttribute("customertypecode") != null) { relType = formContext.getAttribute("customertypecode").getText(); } // Show all first and then hide based on selection formContext.ui.tabs.get('tab_ShipToAccounts').setVisible(false); formContext.ui.tabs.get('tab_CustomerAssets').setVisible(false); formContext.ui.tabs.get('tab_WorkOrders').setVisible(false); formContext.ui.tabs.get('tab_Plants').setVisible(false); formContext.ui.tabs.get('tab_ShipToAssets').setVisible(false); formContext.ui.tabs.get('tab_ShipToWOs').setVisible(false); console.log(relType); if (relType == 'Ship-To') { formContext.ui.tabs.get('tab_CustomerAssets').setVisible(true); formContext.ui.tabs.get('tab_WorkOrders').setVisible(true); formContext.ui.tabs.get('tab_Plants').setVisible(true); } else if (relType == 'Sold-To') { formContext.ui.tabs.get('tab_ShipToAccounts').setVisible(true); formContext.ui.tabs.get('tab_ShipToAssets').setVisible(true); formContext.ui.tabs.get('tab_ShipToWOs').setVisible(true); } } }).call(Sample);
*This post is locked for comments
Thanks Gautam! This was really helpful.
Hi,
You may know that we are attaching JavaScript in the form in various event like on change of field , onload of a form or on save of a form.
Sometimes in our project we may need to create lot of Javascript files/web reaource for different entity. As a result sometimes we may forgot to know which function is for which form ,so to simplify this you can use namespace with some meaningful name where you can include your all JavaScript function for a form or particular entity.
Here in your code it's did similar , I can see there is a namespace defined called "Sample" under that one function included called "showHideTabs". Let say this function you can attach in the form on load so you have to use "Sample.showHideTabs"( with name space name) and pass execution context as first parameter in the event handler.
Inside the function "showHideTabs" we need to get the particular formcontext from the executiinContext parameter to get all control and context information of the form. Have a look below reference for more information-
docs.microsoft.com/.../client-scripting-best-practices
docs.microsoft.com/.../walkthrough-write-your-first-client-script
it's to create a namespace, useful to differentiate from other functions in case they have the same name
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156