7

I have written a GUI extension for SDL Tridion 2011 SP1. The GUI consists of an extra ribbon button and event handler which is triggered when a component is saved.

My event handler is registered as follows:

PowerTools.Commands.ItemCommenting.prototype._execute = function (selection) {
    var item = $display.getItem();
    $evt.addEventHandler(item, "save", this.getDelegate(this._onItemSaved));
    $cme.getCommand("SaveClose")._execute(selection);
};

and the event handler looks like this:

PowerTools.Commands.ItemCommenting.prototype._onItemSaved = function (eventitem) {

    var comment = prompt("Please enter a comment", "");

    $messages.registerNotification("Saving user comments...");

    var commentitemid = eventitem.source.getId();
    var commenterid = eventitem.source.getCreatorId();
    var commenter = $tcm.getItem(commenterid);
    var commentername = commenter.getDescription();
    var commentdate = eventitem.source.getLastModifiedDate();
    var commentversion = eventitem.source.getVersion();

    //Call the service to update 
    PowerTools.Model.Services.AppDataServices.Append("ext:ItemCommenting", commentitemid, "<comment><user>" + commenterid + "</user><message>" + comment + "</message><datetime>" + commentdate + "</datetime><version>" + commentversion + "</version></comment>", null, null, null, false);

};

This is working fine, except that the variable commentername is always undefined. Is there a better approach for getting the name and description of a user?

Additionally, does anyone know if the value returned by eventitem.source.getCreatorId() is actually the Reviser or actually the person who created the item?

Thanks in advance

  • Is it possible getItem acts asynchronously, and you have to hook in a load event or something? – Will Price Mar 8 '13 at 8:25
6

This is the pattern I use when loading items in the CME:

  1. use $models.getItem(item Id) to load objects, heard from someone (@puf?) that this is cached.
  2. check if the object.isLoaded() and if so, execute my event handler
  3. if the object is not loaded, then listen to the event

It all comes down to something like this:

p.keyword = $models.getItem(p.keywordUri);
if (p.keyword.isLoaded()) {
    this._onReleaseKeywordLoaded();
} else {
    $evt.addEventHandler(p.keyword, "load", this.getDelegate(this._onReleaseKeywordLoaded));
    p.keyword.load();
}

You would then call your model's webservice from the event handler, since you're sure the object will be loaded by then.

In your current code you're probably trying to read the description before the object is loaded, hence the undefined. I tend to place variables that I will need across multiple functions in the this.properties var (p in my example) then do something like this at the beginning of every function:

var p = this.properties;
var whatever = p.whatever;
6

You have to use something like this. And they (getcreatorId etc.,) gives you the person who created that item.

PW.Commands.MyTask.prototype._execute = function (selection, pipeline) {  
    var selectedItem = selection.getItems()[0];
    var item = $models.getItem(selectedItem);

    var itemLoad = function (event) {
        var item = this;
        $evt.removeEventHandler(item, "load", itemLoad);

        // variable that you want...
        var commentitemid = item.getId();
        var commenterid = item.getCreatorId();
        var commenter = $tcm.getItem(commenterid);
        var commentername = commenter.getDescription();
        var commentdate = item.getLastModifiedDate();
        var commentversion = item.getVersion();
    };


    if (item.isLoaded()) {
        $evt.addEventHandler(item, "load", itemLoad);
        item.load(true);
    }
    else {
        $evt.addEventHandler(item, "load", itemLoad);
        item.load();
    }
};
  • Welcome KoteswaraRao to Tridion StackExchange and thanks for answering! – Alvin Reyes Mar 8 '13 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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