Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

This is the issue:

  1. I create a ContentBySearchWebpart, specified with my custom Display templates.'
  2. I want to run some other javascript code, after the Display Templates have rendered the content
  3. I then register my custom javascript file in Display template.

            $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Slider.js")'
    

    The problem is that my Slider.js file code executes before the 'Display Templates' Javascript code does.

Can anybody provide me a way to execute my Slider.js code after the display templates have rendered the content of ContentBySearchWebpart(CSWP)?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

:) my issue is solved.

I did following :

  1. Registered the custom JS file in Control_YourTemplateName.html file(like Control_Banner)

    $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/HWScripts/Banner.js");

  2. Added these three lines under the first div after body tag, in YourTemplateName.html ( like Banner.html) file.

     <!--#_  
    

    ctx.OnPostRender = [];

    ctx.OnPostRender.push(function(){ CustomMethodWhichIsIncludedInTheCustomJSFile(); }); _#-->

But a better solution is provided below :

You can skip defining OnPostRender yourself. CBS Display Templates have this function baked in:

<!--#_
   AddPostRenderCallback(ctx, function(){
       alert(ctx.Title + "finished rendering!");
   });
_#-->

There is also AddPreRenderCallback(ctx, function(){});

share|improve this answer
    
Yes it is better :) –  vikas mehta May 6 '13 at 13:16

You can add line of code after including script,

ExecuteOrDelayUntilScriptLoaded( function() { <your custom function name>; }, 'name of   display template ');

for example:

ExecuteOrDelayUntilScriptLoaded( function() { console.dir( SP.Res ); }, 'sp.js');
share|improve this answer
    
This is not what i am asking –  vikas mehta Oct 29 '12 at 6:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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