0

I created the following code in JSlink and connected the file to a site column:

Type.registerNamespace('hiddenFiledContext')
var hiddenFiledContext = window.hiddenFiledContext || {};
hiddenFiledContext.Templates = {};
hiddenFiledContext.Templates.OnPostRender = OnPostRenderFunction;

hiddenFiledContext.Templates.Fields = {
    'TestColumn': {
        'NewForm': FieldRender,
        'EditForm': FieldRender,
        'DisplayForm': FieldRender
    }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(hiddenFiledContext);

The OnPostRender function is triggered correctly. But when I change the defining of the fields to the following example:

hiddenFiledContext.Templates = {
Fields: {
    'TestKolom': {
        'NewForm': FieldRender,
        'EditForm': FieldRender,
        'DisplayForm': FieldRender
    }
 }
};

The OnPostRender function isn't triggered. What's the reason why the function isn't triggered that way?

I came up with the not working notation because of an example of "Office DEV-PNP": https://github.com/SharePoint/PnP/blob/master/Samples/Branding.JSLink/Branding.JSLink.NCSS/Style%20Library/OfficeDevPnP/Branding.JSLink/TemplateOverrides/TaxonomyOverrides.js

1

Because during the execution of your code, you are slowly building up the hiddenFieldContext object. First, when you create it, it's empty. Then, you build on it and tell it that it has a property Templates that's also empty:

hiddenFieldContext.Templates = {};

Then you add on to the Templates property, and tell it that it has a child property of OnPostRender, and OnPostRender refers to a function:

hiddenFieldContext.Templates.OnPostRender = OnPostRenderFunction;

But, as soon as you do

hiddenFieldContext.Templates = {
    Fields: { }
}

you have effectively "erased" the OnPostRender property that you had just added. You are saying "ok, forget that I just added OnPostRender to Templates, now Templates equals this other object structure".

If you want to structure your code in the way of your second example, you will have to do:

hiddenFieldContext.Templates = {
    OnPostRender: OnPostRenderFunction,
    Fields: {
        // your field stuff here
    }
}

Here is a good primer on working with Javascript objects:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.