Wednesday, June 28, 2017

Closure in JavaScript

“…an inner function always has access to the
vars and parameters of its outer function, even
after the outer function has returned.”
-- Douglas Crockford

In Non Closure variable value will be lost once function return. And whenever it is called it will be assigned new value. Below is example of non closure.

function NonClosureFunct(){
var counter = 0;
counter += 1;
return counter;
}

Closure  help to address this problem and retain value of variable even after return from function.Variable stays around even after function returns.

function ClosureFunct(){
var counter = 0;
return function(){ counter +=1;};
}

Monday, June 12, 2017

SPUtility.js is a JavaScript library used to make modifications to SharePoint's list forms

SPUtility.js is a JavaScript library used to make modifications to SharePoint's list forms (NewForm.aspx and EditForm.aspx in a survey, custom list or library). It works with SharePoint 2007, 2010, and 2013 (including SharePoint Online / Office 365).
SPUtility.GetSPField('Title').SetValue('Hello world!');
Features:
  • Set or get field values in Document Libraries, all sorts of List forms (custom lists, issues, etc), and survey
  • Make a field read only
  • Hide a field from view
  • No server side code to deploy!
This can be used for:
  • Preventing a user from selecting a value when an item is initially created (ex: Status must be PENDING when the item is created)
  • Hiding fields from a submitter but allowing the field to be edited (or vice versa).
  • Auto-populating the form using a URL with query parameters
  • Auto-populate a list column with a sequential number
  • Hide/Show field on list form based on value from another field

Thanks to authors for making this wonderful utility.