Thanks for coming in :-). The title of this post is long, but I needed to get this right. I am trying to produce a .js file that I can link to via JS link on a List View Web Part that is displayed on a Wiki page. I have culled the web fairly deeply on this and have found MANY bits and pieces for the job, but nothing all-together yet. Maybe we can get this done for our “people”. Here are the requirements I want to satisfy:
- Open List View field of type Hyperlink in a modal window on-click; NOT “Title” field
- Referenced Info…Â – JasonGuo’s post on the JSLink…
- Implement proper Minimal Download Strategy (MDS) for the page; best practice & performance
- Referenced Info… – I want to implement the Namespace-type of script per his example; MDS appears to mandate it’s use.
- Implement targeted behavior so that the JS link applied to the web part will not alter other web parts on the dashboard
I am just now cutting my teeth on JavaScript, as it looks like the future of SharePoint development, and I am not able to get this worked through so far :-(. I think the web could really benefit on a holistic approach to this all saved in one place. Up for the challenge?
Here is my code attempt:
//Handle MDS Garbage Collection via MS Ajax
Type.registerNamespace(‘Alan’)
//Declare Namespace for JS
Alan.Demos=Alan.Demos || {};
Alan.Demos.Templates=Alan.Demos.Templates || {}
Alan.Demos.Functions=Alan.Demos.Functions || {}
Alan.Demos.Functions.Display = function (context) {
//var itemLink=context.CurrentItem.link
var viewModal=context.CurrentItem.link;
var itemID=context.CurrentItem.ID;
var itemTitle=context.CurrentItem.Title;
var url=context.CurrentItem.link;
var urlDesc=context.CurrentItem[‘link.desc’];
return “<a href=’#’ onclick=\”viewModal(‘”+url+”‘,'”+itemTitle+”‘)\”>” +urlDesc+ “</a>”;
}
Alan.Demos.Templates.Fields = {
‘link’: {
‘DisplayForm’: Alan.Demos.Functions.Display,
‘View’: Alan.Demos.Functions.Display,
‘NewForm’: null,
‘EditForm’: null
}
}
Alan.Demos.Functions.viewModal = function (url,title) {
var options = {
title: “Item: “+title,
//Undefined width & height = autosized modal
//width: 600,
//height: 600,
url: url
}//;
SP.UI.ModalDialog.showModalDialog(options);
//Â Â Â return false;
}
Alan.Demos.Functions.RegisterField = function () {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(Alan.Demos)
}
Alan.Demos.Functions.MdsRegisterField = function () {
var thisUrl = _spPageContextInfo.siteServerRelativeUrl
+ “zWebDev.js”;
Alan.Demos.Functions.RegisterField();
RegisterModuleInit(thisUrl, Alan.Demos.Functions.RegisterField)
}
if (typeof _spPageContextInfo != “undefined” && _spPageContextInfo != null) {
Alan.Demos.Functions.MdsRegisterField()
}
else {
Alan.Demos.Functions.RegisterField()
}
I keep getting the error that viewModal is undefined, but else seems to work. Please give me a hand. Thanks!
Have you checked that the url generated in this line is correct?
 var thisUrl = _spPageContextInfo.siteServerRelativeUrl
       + “zWebDev.js”;
I would think you need to put a little more in the path. i.e if your file was in Site Assets something like +”/SiteAssets/zWebDev.js”
If you put a breakpoint on the line you can then inspect the value it gets, if it doesn’t match the file’s path then that will be the reason it breaks with MDS page changes.
Now the modal is working, thanks Paul! The MDS seems to not be working. I went into the script and changed something and went back to the page and refreshed it without using the browser button (clicked on the Wiki Page Title instead) and the change didn’t appear. Using the browser refresh made the change appear. The change was to add some text to the title: “Item: “+title, line. This is the type of change I was under the impression the MDS code was supposed to fix. Any thoughts?
Very Cool!!! Thank you!!! That looks to remedy the modal. I’m working on the rest of the post. Thanks for helping. The entire solution will be here when it is finished 🙂
I hope you, and/or others will be around for more questions.
You are calling viewModal in your onclick after setting it to the URL. I think you meant to include the namespace: Alan.Demos.Functions.viewModal.
