SharePoint: Using JScript in calculated column (field) formula via CSR and JSLink

 

Introduction and problem 

Calculated fields formulas are user-friendly and easy-to-start-using business logic containers in SharePoint.While the formulas are very spread there are some limitations and confusions like the "today" problem.

Numerous articles address the issues and use CSR (client-side rendering) as a convenient and recommended way to change the behavior at client rendering time.

The current article is intended to collect P&P (patterns and practices) for SharePoint calculated fields implementation via CSR and JSLink.
 

Sub problem: long-running calculations

One of the common problems while using CSR is a disability of direct access for field values that are not included in the view. A direct solution is to use CSOM (client side object model) to access the missing values but at the rendering time CSOM is not accessible and although we can still reach the values via synchronous REST ajax request it is not a recommended way and we are getting corresponding depreciation warning:

In the current article, we consider the CSR approach to implement common field value JScript calculation with long calculations(external requests) and use JSLink to implement the solution.  As an example of external request REST ajax calls to reach missing field values are considered. The approach for external requests is an alternative for using SharePoint external columns.

Solution 

The idea is pretty simple. We want to have minimal impact on the main rendering performance and we want to finish overall rendering as soon as possible.

Solution:

  • Starting the calculations as soon as possible. Putting it in the JScript queue or in the HTML5 web workers right after getting all the input information for the calculation. In our case it is the itemid which we reach at the field override rendering event.
    -We render the calculated values in onpostrender event.

Code 

coming...

Restrictions and Usage

Restr

Resources

Combining jQuery Deferred with the HTML5 Web Workers API - CodeProject
[SP2013] Loading indicator images
Sharepoint: Introduction to external data

Web Workers browsers support

See Also 

Product Documentation
Calculated Field Formulas
How to: Customize a field type using client-side rendering
Customize a list view in SharePoint Add-ins using client-side rendering
How to: Extend the Geolocation field type using client-side rendering

Community
*note from the author (and it is mentioned in several articles as well): a lot of authors use JSLink and CSR terms interchangeably (more often JSLink instead of CSR) what is wrong but the original titles are kept below. 
Is it possible to refer to a field hidden in current view from JSLink attached to current view
Show content of list field in another field using JSLink
SP 2013: Using the JSLink property to change the way your field or views are rendered in SharePoint 2013
Show remaining days in task list
Custom Field Rendering with JSLink
SharePoint 2013 Client Side Rendering: List Views
SharePoint 2013 Client Side Rendering: List Forms + Layout - CodeProject

SharePoint 2013 Client Side Rendering: List Forms + KnockoutJs - CodeProject

SharePoint 2013 Client Side Rendering: List Forms - CodeProject
SharePoint 2013 JSLink Tutorial and Examples
Office365/SharePoint VIEW One-Click Productivity Enhancements
JS Link – Highlighting a Row with CSR – Wes Preston
GitHub - SharePoint Calculated Column Editor
The Truth about using [Today] in SharePoint Calculated Columns
2013 - How to use Today and Me in Calculated column - SharePoint Stack Exchange
SharePoint: Creating Calculated Column Formulas the Easy Way Using Excel
SharePoint - Using JSLink with SharePoint 2013