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?
I normally follow this approach in Anguilla:
$models.getItem(item Id)
to load objects, heard from someone (@puf?) that this is cached.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;