Useful JavaScript Function – Default parameters within your JavaScript functions #in #SharePoint #SP2010 #PS2010 #ProjectServer
So it has been a while since I have posted anything on the blog.
As I am sure many of you can relate, there comes a time in all projects when you have to concentrate solely on them to ensure that all factors play out as expected. One of those times would be go live time.
The project I have been working on for the last year is about to go into trial with its first division, so it has been heads down to ensure that all bug fixes and business intelligence data is correct.
As of today, we are officially at a code freeze and we are clearing down the databases to rid them of test data ready for migration to the Production environment and the final integration testing process.
As a result, we can commence blogging again, of which I have a few topics stored up.
First up is…
Default parameters in a JavaScript function
Project Scenario: I created a generic function to contact the User Profile service and set some default global variables when the page loads for the current logged in user.
This had to be extended to cope with me passing a different AD User Name (SAMAccountName) and return the details in a JavaScript object.
So the basic mechanism was there already.
- I was passing the current user as a variable
- User Profile Service was already being queried
- Set the global variables for the current user
I wanted to extend this to take any user, but not upset the other functions that relied on the global variables I was setting in the function.
So essentially I added a setGlobal flag parameter and provided a default to the function so that existing code could work (surrounded by the newly created flag of course) and then implemented what I needed to.
This allowed my existing code to continue to function without issue, but also allowed me to reuse my User Profile functionalities for another purpose.
Examples:
The original idea came from the following blog post:
In my code, I implemented the first method. In future however I shall certainly use the second approach.