Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle loading of a view in Tridion's CME

Tags:

tridion

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?

like image 944
Frank van Puffelen Avatar asked Oct 15 '12 17:10

Frank van Puffelen


2 Answers

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");
}
like image 197
johnwinter Avatar answered Oct 30 '22 22:10

johnwinter


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.

like image 28
Boris Ponomarenko Avatar answered Oct 30 '22 22:10

Boris Ponomarenko