表示テンプレートがコンテンツをレンダリングした後に他のJavaScriptを実行します。
-
09-12-2019 - |
質問
これは問題です:
- カスタム表示で指定されたContentBySearchWebPartを作成します。 テンプレート '
- ディスプレイの後、他のJavaScriptコードを実行したい テンプレートはコンテンツをレンダリングしました
-
その後、私のカスタムJavaScriptファイルを表示テンプレートに登録します。
.$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Slider.js")'
問題は、my slider.jsファイルコードが を実行することです。 テンプレートの「JavaScriptコードは。
誰かが私にslider.js code を実行する方法を私に提供することができます 表示テンプレートは、の内容をレンダリングしました ContentBySearchWebPart(CSWP)?
解決
:) my issue is solved.
I did following:
Registered the custom JS file in Control_YourTemplateName.html file(like Control_Banner).
$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/HWScripts/Banner.js");
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(){});
他のヒント
スクリプトを含めてコード行を追加できます。
ExecuteOrDelayUntilScriptLoaded( function() { <your custom function name>; }, 'name of display template ');
.
例えば:
ExecuteOrDelayUntilScriptLoaded( function() { console.dir( SP.Res ); }, 'sp.js');
.