题
这是问题:
- 我创建了一个contentbysearchwebpart,用我的自定义显示指定 模板。'
- 我想在显示后运行其他一些JavaScript代码 模板呈现了内容
-
我然后在显示模板中注册我的自定义JavaScript文件。
.$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Slider.js")'
问题是我的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');
.