Ejecute cualquier otro javascript después de que las plantillas de pantalla hayan presentado el contenido.

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/49349

  •  09-12-2019
  •  | 
  •  

Pregunta

Este es el problema:

  1. creo un contentbysearchwebpart, especificado con mi pantalla personalizada plantillas '
  2. Quiero ejecutar algún otro código JavaScript, después de la pantalla Las plantillas han prestado el contenido
  3. Luego, registre mi archivo JavaScript personalizado en la plantilla de pantalla.

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

    El problema es que mi código de archivo slider.js ejecuta antes de la pantalla El código JavaScript de las plantillas hace.

    ¿Puede alguien proporcionarme una manera de ejecutar mi código slider.js después de el Las plantillas de pantalla han prestado el contenido de CONTENTBYSEARDWEBPART (CSWP)?

¿Fue útil?

Solución

:) 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(){});

Otros consejos

Puede agregar línea de código después de incluir script,

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

Por ejemplo:

ExecuteOrDelayUntilScriptLoaded( function() { console.dir( SP.Res ); }, 'sp.js');

Licenciado bajo: CC-BY-SA con atribución
scroll top