28
May 2017

Best practices for writing JavaScript for Project Online – Part 2


Checking the Project details page edit mode


The tip is about changing script behaviour based on the fact that project is editable or not.

I recently created a custom editable grid for user to enter project financial information. However, I wanted the grid to behave like regular PDP input fields which does not allow editing if the project is not in Edit mode. See the output of my grid in Edit and non-edit mode.

When PDP is in edit mode



When PDP is not in edit mode


On every PDP, project online PDP infrastructure injects a global object named EditState which exposes some useful information about the current edit state of the PDP. One of its property EditState.Editing is set to true when the page is in edit state. See the following example code.

_spBodyOnLoadFunctionNames.push("myScriptStartup");
function myScriptStartup () {
var editMode = EditState.Editing;
if (editMode) {
// PDP is in edit mode
// Do things differently here
console.log('PDP is in edit mode. so our input fields will be editable');
}
else {
// make custom input fields read-only
}
}
view raw pdpEditmode.js hosted with ❤ by GitHub

In the next part of this series, I’ll talk about how we can avoid a common Internet Explorer error

0 comments:

Post a Comment