Call same client Method from different locations (Action, CUI , Form button)

Hi, I wonder what´s the best way to call the same Client Methods from a different locations. The item that triggers the Method needs different handling based on the event used as trigger. A standard ItemType "Action" Method may work like this:
var inn = this.newInnovator()
var myItem = this; // passed with Action
return aras.AlertError("This is " + this.getProperty("name"));
For a Form button onClick this will work:
var myItem = parent.thisItem; // form call
return aras.AlertError("This is " + myItem.getProperty("name"));
CUI elements seem to need an additional call for the Item
var id;
var topWindow = aras.getMostTopWindowWithAras(window);
if (topWindow.work && topWindow.work.grid) 
{ 
    // in case Method is triggered from Main-Window-
    var workFrame = topWindow.work; 
    id = workFrame.grid.getSelectedID();
} 
else 
{
    // in case Method is triggered from Tearoff Window
    id = thisItem.getID();
}

var myItem = inn.getItemById("/*Itemtype*/", id);
return aras.AlertError("This is " + myItem.getProperty("name"));
Form and Actions can be unified in one Method this way:
var myItem;
if (parent.thisItem === undefined){
    myItem = this; // Action call
}
else 
{
    myItem = parent.thisItem; // form call
}

return aras.AlertError(myItem.getProperty("name"));
But I wonder if it´s possible to extend this construct to CUI elements? Background: I have a few Actions, that I want to move to CUI elements. But I don´t want to change anything adhoc. So I would prefer to have more flexible Methods that I can reuse more easily. Thx! Regards Angela
Parents
  • Hi Angela, The sample I wrote is just one possible implementation, but the goal is to make the way you call GetItemFromWindow as generic as possible. window is actually a JavaScript object that can give us information about where the method is being called from, so the methodArgs at least in this sample should always look like  var methodArgs = { contextWindow: window }. The GetItemFromWindow function should then be able to figure out what the context is based on what properties it has access to from the contextWindow. To answer your second question, I don't believe that you have access to the item itself from the context of the cui button, so the getItemById call is necessary. Chris
    Christopher Gillis Aras Labs Software Engineer
Reply
  • Hi Angela, The sample I wrote is just one possible implementation, but the goal is to make the way you call GetItemFromWindow as generic as possible. window is actually a JavaScript object that can give us information about where the method is being called from, so the methodArgs at least in this sample should always look like  var methodArgs = { contextWindow: window }. The GetItemFromWindow function should then be able to figure out what the context is based on what properties it has access to from the contextWindow. To answer your second question, I don't believe that you have access to the item itself from the context of the cui button, so the getItemById call is necessary. Chris
    Christopher Gillis Aras Labs Software Engineer
Children
No Data