Make javascript code work with Minimal Download Strategy Part 1
By Anatoly Mironov
I have a newer blog post about MDS, that provides a much simpler solution. Please check it before reading further.
This is a part 1 of the blog post about Minimal Download Strategy and javascript adjustments for user code. What I initially thought should be enough for one post, is not enough, so I see it as a part 1. I wrote this post after I had read Chris O’Brien’s post about JSLink Here I want investigate how we can get his accordion list view working with MDS. Minimal Dowload Strategy or MDS is a new feature in SharePoint 2013. By now, if you read this post, you already know about it. The simplest way to see if MDS is enabled on your site, you can recognize it on the “ugly” urls. I don’t think they are so ugly. But it is a matter of taste and habit. No matter if you like MDS or not, MDS is enabled on many site templates and is a huge step towards a faster, more responsive architecture in SharePoint, I would say, towards the Single Page Application concept in SharePoint (but it is a long way to go). We have to keep the MDS in mind, when we write our customizations in javascript. SharePoint 2013 loves javascript and the probability is high that you write a lot of javascript. If it doesn’t work with MDS, your code breaks and the user doesn’t see the functionality, or the site owner must disable the Minimal Download Strategy feature. I wouldn’t like to have disabling of an improvement feature as a prerequisite for my code. In this blog post I want to dig into the techniques for getting the javascript code working with MDS. For a while ago I read a wonderful blog post in Chris O’Brien’s blog:
There he describes how JSLink works and how much you can change a standard XSLTListViewWebPart. Chris creates a jQuery UI Accordion view for his list view. As an issue he mentions the MDS. Here I want to take Chris’ code and adjust it for MDS. My goal is to change as little as possible to find the most important steps for MDS. So I’ll continue where he has finished. My colleages who have debugged the MDS a lot, gave me a tip: $_global. The SharePoint 2013 internally uses function inside the files which starts with $_global: Here we have callout.js/callout.debug.js The function is called $_global and _ and the filename callout = $_global_callout. Then the function is invoked directly in the end of the file. It is a different story than the anonymous self executing funcitons we’ve seen before. When I search the hive folder with grepWin tool, I find 148 files containing “$_global”: I rewrote the the code into one wrapper function and invoked in the end of file: [sourcecode language=“javascript”] // function to setup JSLink templates function $_global_AccordionListView() { // function to process an accordion item.. window.COB = window.COB || {}; window.COB.accordionItem = { customItemHtml: function (ctx) { var accordionItemHtml = “ ” + ctx.CurrentItem.Title + “ “; accordionItemHtml += “ ” + ctx.CurrentItem.AccordionItemDescription + “ “; return accordionItemHtml; } }; var overrideCtx = {}; overrideCtx.Templates = {}; overrideCtx.Templates.Header = “ ”; overrideCtx.Templates.Item = window.COB.accordionItem.customItemHtml; overrideCtx.Templates.Footer = “ “; overrideCtx.BaseViewID = 1; overrideCtx.ListTemplateType = 11000; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx); $(document).ready(function() { // It seems SharePoint inserts a script tag in an inconvenient place that breaks jQuery UI’s accordion, so let’s remove it! // (N.B. further testing recommended for production).. $("#accordion”).find(’#scriptBodyWPQ2’).remove(); $("#accordion”).width(‘70%’); $("#accordion”).accordion(); }); } $global_AccordionListView(); [/sourcecode] Unfortunately, it didn’t help. There was still no accordion. But wait, is it just the accordion that isn’t created. Indeed. The JSLink itself works. We can see it in the markup: Strange, maybe there was no need for rewriting the code in this case. I changed back all the javascript code to see the markup. It is the right markup. Then the problem is the accordion initialization, or the $(document).ready. Then I thought about the SharePoint-function for that: _spBodyOnLoadFunctionNames and rewrote the $(document).ready: [sourcecode language=“javascript”] function onReady() { $("#accordion”).find(’#scriptBodyWPQ2’).remove(); $("#accordion”).width(‘70%’); $("#accordion”).accordion(); } _spBodyOnLoadFunctionNames.push(“onReady”); [/sourcecode] When I deployed it, it worked… It doesn’t seem like it is the whole solution. It is too simple. Well, it is the solution for the Accordion List View. By putting the accordion initialization code into _spBodyOnLoadFunctionNames we ensure that SharePoint runs it even on pages with MDS. As the name tells us: OnLoad. This appends the code to the onload function which runs after the $(document).ready. It means the time before the text becomes an accordion is longer.
Other cases
Allright, the actual jslink works pretty fine with MDS, except the accordion. But if we hadn’t the jQuery UI Accordion, there wouldn’t be a need to make change to the javascript code. There must be other cases where we need to adjust our javascript code. In the meanwhile I discovered a couple of files in the hive folder which use RegisterModuleInit function: After a quick search I found this:
- Register CSR-override on MDS enabled SharePoint 2013 site in the Sridhar’s blog on MSDN blogs.
Sridhar writes in his post, you have to have your javascript code in a function, then call the function inside a RegisterModuleInit. It is strange. Chris O’Brien’s example makes almost the same thing as Sridhar, it changes the display with JSLink. But there was no need for RegisterModuleInit. More investigation will be in part 2. Thanks to my colleagues Christopher, Björn and Martin for giving me tips and discussing it with me.
Comments from Wordpress.com
Custom JavaScript and MDS: Some rules I learned | Geek Time - Jun 2, 2014
[…] Make javascript code work with Minimal Download Strategy Part 1 and Part II […]
[…] Make your javascript code work with MDS part 1 […]
- Code Sample
- Customaction
- Javascript
- JQuery
- JQuery UI
- Jslink
- List
- Mds
- Minimal Download Strategy
- Sharepoint
- Sharepoint 2013