I have an extension to the Tridion 2011 Content Manager Explorer where I want to execute a specific piece of JavaScript only for a specific view.
I do this with the following JavaScript fragment:
var onDisplayStarted = function () {
$evt.removeEventHandler($display, "start", onDisplayStarted);
if ($display.getView().getId() == "PublishPopup") {
...
}
};
$evt.addEventHandler($display, "start", onDisplayStarted);
This code has worked well in the past and it definitely triggers when the PublishPopup
opens and ensures my code only executes in that specific view.
But unfortunately I sometimes get the following error message in the JavaScript console while loading the DashboardView
:
Uncaught TypeError: Object # has no method 'getId'
The error doesn't cause any problems, most likely since errors in event handlers are handled correctly by Tridion's UI framework. But I'd still prefer to not have the error showing in the JavaScript console.
I understand how to detect if the getId
method exists:
if ($display.getView().getId && $display.getView().getId() == "PublishPopup") {
But that would just mean that the code doesn't execute ever. And although it seems to work fine in the PublishPopup
right now, I'd rather know the proper way to handle this type of "my code should execute once the view has initialized" sequence.
Does anyone know a better way to handle this?
Do you have code in your configuration to only include your extension on the popup window? For example:
<cfg:extension target="Tridion.Web.UI.Editors.CME.Views.Popups.Publish">
I typically use that configuration to only run script on a specific view within the cme.
My js file then looks like this:
$evt.addEventHandler($display, "start", onDisplayStarted);
function onDisplayStarted() {
$log.message("stuff here");
}
Well, there are two different views, which are loaded at the same time - Dashboard View and Tridion Dashboard View. Tridion Dashboard View is what you see when you click on SDL Tridion tab in Ribbon Toolbar. Indeed, this view doesn't have getId method (which is strange, btw). That's why you have this issue.
Nevertheless, the whole idea behind the file groups in configuraton file is to minimize amount of the javascript, loaded for each view and to minimize un-needed javascript processing. So I would recommend to split your javascript file into pieces and load them only oт necessity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With