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.
- How do I check if a Component is the parent or a copy of the parent using the Tridion JavaScript Framework (Anguilla).
- How to use Tridion JavaScript Framework (Anguilla) to get the workflowInfo for a Component?
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.
isEnabled()
function is perhaps a bit heavy on the UI, since that function is called quite a lot of times in the background.isAvailable()
function beforeisEnabled()
filters, so that only components trigger the full load item.item.load()
method being asynchronous, so I don't think there is a way to wait for its result. Probably better to simply returntrue
fromisEnabled()
and handle the exceptions in theexecute()
call.