Skip to main content
3 of 4
some grammar fixes.
Glenn Stevens
  • 1.9k
  • 2
  • 18
  • 33

How to use Tridion JavaScript Framework (Anguilla) to wait for all the component information

How do I resolve the issue of the browser being asynchronous and each new event being added to the event stack?

In my previous question it was suggested that, because of caching, I should wait until the full component. Also this answer was giving to Robert in another topic, see below.

My current code looks like this now;

/// <summary>
///
/// </summary>
/// <returns>boolean</returns>
Extensions.Unlocalize.prototype.isEnabled = function Unlocalize$isEnabled(selection) {
    var item = $models.getItem(selection.getItem(0)),
        itemTCM = item.getId(),
        pubTCM = null,
        isAvailable = false,
        itemRegex = /.?tcm:(.*?)\-/,
        pubRegex = /.*?\-(.*)-.*/;

    console.log("Extensions.Unlocalize.prototype.isEnabled; Started."); 
    console.log("Info of parameter item at start: ", item.getInfo());     

    //Make sure the component is loaded before using getOwningPublicationId();
    var onItemLoaded = function Extensions$Unlocalize$_onItemLoaded() {
        console.log("Extensions$Unlocalize$_isEnabled; Started."); 
        $evt.removeEventHandler(item, "load", onItemLoaded);
      
        var itemInfo = item.getInfo();
        console.log("Info of parameter item in eventHandler: ", itemInfo);
    
        pubTCM = item.getOwningPublicationId(); 
        pubTCM = pubTCM.match(pubRegex);
        itemTCM = itemTCM.match(itemRegex);
    
        if (itemTCM[1] == pubTCM[1]) {
            isAvailable = true;
        }        
        console.log("Extensions$Unlocalize$_isEnabled; ", isAvailable);         
    };

    console.log("Extensions.Unlocalize.prototype.isEnabled; addEventHandler"); 
    $evt.addEventHandler(item, "load", onItemLoaded);

    console.log("Extensions.Unlocalize.prototype.isEnabled; item.load()"); 
    item.load(item.isLoaded(true), $const.OpenMode.VIEW);

    console.log("Extensions.Unlocalize.prototype.isEnabled; Return."); 
    return isAvailable;
}

This works all good, the only issue is the isEnabled function is finished before _onItemLoad is done, this way return isAvailable is always false. Where I would like to check if the component is in the same publication created as being selected.

The outcome of the console looks like this:

Extensions.Unlocalize.prototype.isEnabled; Started. 
Info of parameter item at start: Tridion.ContentManager.ItemInfo {ID: "tcm:24-88", Title: undefined, Icon: "T16L0P0", ItemType: "tcm:16", ItemTypeName: "Component"…}
Extensions.Unlocalize.prototype.isEnabled; addEventHandler 
Extensions.Unlocalize.prototype.isEnabled; item.load() 
Extensions.Unlocalize.prototype.isEnabled; Return. 
Extensions$Unlocalize$_isEnabled; Started.
Info of parameter item in eventHandler: Tridion.ContentManager.ItemInfo {ID: "tcm:24-88", Title: "Pie dessert wypas", Icon: "T16L0P0", ItemType: "tcm:16", ItemTypeName: "Component"…}
Extensions$Unlocalize$_isEnabled;  false 

This is of a component that is not selected in the publication it was created. If I select a component that is created in this publication the last log will be true.

How do I resolve the issue that the item is always disabled(false) in my menu.

user219