Book a Demo

Author Topic: Javascript AddIn - EA_OnContextItemChanged  (Read 10656 times)

sjf

  • EA User
  • **
  • Posts: 35
  • Karma: +1/-0
    • View Profile
Javascript AddIn - EA_OnContextItemChanged
« on: June 30, 2023, 02:24:46 am »
Hi,

I am trying to build an EA Javascript AddIn following the example in the EAExample project.

I am firstly trying to get the GUID of the item selected. The signature of EA_OnContextItemChanged defines 3 parameters:
  • Repository (EA.Repsoitory)
  • GUID (String)
  • ot (EA.ObjectType)

My first example just takes the GUID and attempts to print it out to the System Output Window.
This is the error I receive:

--------------------------------------------------
Invocation error in: addin.EA_OnContextItemChanged
------------------------------------------------
  24:    }
  25:    ExampleAddIn.prototype.EA_OnContextItemChanged = function(Repository,GUID,ot) {
  26:   
  27:    Session.Output(GUID);             [!!!! Parameter 1 type mismatch]
  28:    }
  29:    var addin = new ExampleAddIn();
  30:    var _pa_ = [];
--------------------------------------------------

Any help or suggestions would be much appreciated.
« Last Edit: June 30, 2023, 06:34:47 pm by sjf »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Javascript AddIn - EA_OnContextItemChanged
« Reply #1 on: June 30, 2023, 04:07:24 am »
Are you sure there's a session object available?

Maybe try with a messagebox, or Repository.WriteOutput?

Geert

sjf

  • EA User
  • **
  • Posts: 35
  • Karma: +1/-0
    • View Profile
Re: Javascript AddIn - EA_OnContextItemChanged
« Reply #2 on: June 30, 2023, 09:19:32 am »
Geert,

When I Session.Output("Some Text") it correctly prints the message in  the System Output window.

If i change this to:
Session.Output(typeof GUID);

it outputs 'object'

Thanks
Steve
« Last Edit: June 30, 2023, 05:55:14 pm by sjf »

sjf

  • EA User
  • **
  • Posts: 35
  • Karma: +1/-0
    • View Profile
Re: Javascript AddIn - EA_OnContextItemChanged
« Reply #3 on: June 30, 2023, 08:44:20 pm »
Then only way I could get the information I needed was to use:

var selection = Repository.CurrentSelection;

for(var i = 0; selection.List != null && i < selection.List.Count; i ++)
{
   var context = selection.List.GetAt(i);
   
   Session.Output(context.ElementGUID);
}
« Last Edit: June 30, 2023, 08:46:02 pm by sjf »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Javascript AddIn - EA_OnContextItemChanged
« Reply #4 on: June 30, 2023, 09:16:02 pm »
Have you read this: https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/modeladdins-code.html

Quote
Retrieving return values in JavaScript
When handling a reception for an event with OUT/INOUT parameters, values must be read and assigned using the .val attribute of those parameters.

For example, to set the value of the TagValue parameter on the EA_OnElementTagEdit event:

     TagValue.val = "Hello World!"

Looks like you should use GUID.val

Geert

sjf

  • EA User
  • **
  • Posts: 35
  • Karma: +1/-0
    • View Profile
Re: Javascript AddIn - EA_OnContextItemChanged
« Reply #5 on: June 30, 2023, 10:46:44 pm »
Have you read this: https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/modeladdins-code.html

Quote
Retrieving return values in JavaScript
When handling a reception for an event with OUT/INOUT parameters, values must be read and assigned using the .val attribute of those parameters.

For example, to set the value of the TagValue parameter on the EA_OnElementTagEdit event:

     TagValue.val = "Hello World!"

Looks like you should use GUID.val

Geert

Thanks Geert, great spot.