3

I would like to use the .load() method in a Tridion Bookmarklet for the Schema Title here.

However, it currently does not display the Schema Title the first time. It does work the 2nd time.

I know I should use a callback for it, but I do not know if it is possible to do it in a Bookmarklet. I tried to follow the example here, but no luck.

2 Answers 2

2

Quick and dirty modification:

(function (UI, B, e, a, r, d)
{
    try
    {
        while (r = UI.frames[e++])
        {
            if ((a = r.$display && r.$display.getView()) && a.getId() == B)
            {
                d = a;
                break;
            }
        }

        var onSchemaLoaded = function()
        {
            m1.$evt.removeEventHandler(schema, 'load', onSchemaLoaded);
            alert('Schema URI: ' + schema.getId() + ', Title: ' + schema.getTitle());
        }

        var m1 = window.top.frames[1];
        var myitem = d.getMainInterface().getListSelection();
        var comp = m1.$models.getItem(myitem.getVersionlessIds()[0]);
        var schema = comp.getSchema();
        m1.$evt.addEventHandler(schema, 'load', onSchemaLoaded);
        schema.load();
    }
    catch (x) {};
})(window.top, 'DashboardView', 0)

Alternatively, if you only want the ID and Title of the Schema, you could simply load it from the Component XML. You would need to load the Component instead, then, but at least you can be reasonably certain the the user is allowed to read the Component if he can select it (whereas he might not have access to the Schema).

Edit: Here's an example of the alternative solution I mentioned:

(function ()
{
        try
        {
            var frame;
            var view;

            function initialize()
            {
                var topFrame = window.top;
                var index = 0;
                var v, currentFrame;

                while (currentFrame = topFrame.frames[index++])
                {
                    if ((v = currentFrame.$display && currentFrame.$display.getView()) && v.getId() == 'DashboardView')
                    {
                        view = v;
                        frame = currentFrame;
                        return;
                    }
                }
            }

             function onComponentLoaded()
            {
                if (load) evt.removeEventHandler(comp, 'load', onComponentLoaded);
                var xmlDoc = comp.getXmlDocument();
                var schemaId = xml.getInnerText(xmlDoc, "/tcm:Component/tcm:Data/tcm:Schema/@xlink:href");
                var schemaTitle = xml.getInnerText(xmlDoc, "/tcm:Component/tcm:Data/tcm:Schema/@xlink:title");
                if (load) comp.invalidateEditableProperties();

                alert('Schema URI: ' + schemaId + '\nSchema Title: ' + schemaTitle);
            }

            initialize();

            var evt = frame.$evt;
            var models = frame.$models;
            var xml = frame.$xml;

            var selectedItemId = view.getMainInterface().getListSelection().getVersionlessIds()[0];
            if (!selectedItemId) return;

            var comp = models.getItem(selectedItemId);
            var load = !comp.isLoaded();

            if (load) 
            {
               evt.addEventHandler(comp, 'load', onComponentLoaded);
               comp.load();
            }
            else
            {
               onComponentLoaded();
            }
        } 
        catch (x) {};
})()

It handles the case where the Component has to be loaded and then unloaded again afterwards. It can certainly be made less verbose, to your liking.

3

As soon as you got your application window (appWindow), you can use

$display.staticLoadItem(itemId, onLoadCallBack);

within the appWindow

with ((function getAppWindow(UI, Be, ard) {
    while (!(ard = UI[Be++].Tridion)){}; return ard.getApplicationWindow();
})(window.top.frames, 0))
{
    $display.staticLoadItem("tcm:34-847-8", function item$onStaticLoaded(itemId, isLoadSuccess)
    {
        isLoadSuccess && alert($models.getItem(itemId).getStaticTitle());
    });
}

and, NO, normally I`m not using constructions such this in day-to-day development.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.