Modify Type Script

Provides utilities for modifying a type from a member type node type derivation script.

ModifyType.modifyScript(options)

Modify a script method associated with the type to include additional code.

The additional code can be specified as a node and optional method to be called, as a block of script, or as a function.

Options are:

contextMemberType Context member type to pass to new code. Defaults to application.get('contextMemberType'), which is always set in a member type node type derivation script.
method The script method to modify, for example 'system.TYPE_TRIGGER_SCRIPT' to specify a trigger script. Defaults to system.NODE_SCRIPT.
position If the script method already exists on the type, where to insert the new code. Set to "start" or "end", default is "end". If a position of "start" is set, the code will be inserted before the type-level code but after code already inserted by other member types.
call Node to call for new functionality. 
callMethod Method on the call node to invoke. Defaults to system.NODE_SCRIPT. 
script Text of script to run. 
fn Function the text of which should be called.
placeholders An object. If the script or fn contains a property in this object in the form ${property}, the string is replaced by the value of the property. This allows you to add node reference in literals using ${ref}, and then replace them with real node references.

The new functionality is only called if there are no application errors.

call, script and fn are mutually exclusive, and callMethod can only be set if call is used.

If call is used the application attribute contextMemberType will be set.

If a function is used, the text of the function will be included. The function will be passed the contextMemberType. Global variables and function used from the function will be evaluated within the type method, so the function must be completely self-contained.

If script is used, the script wil be embedded in a function and called as if a function were passed. contextMemberType will be passed to the function.

The method will raise an exception if it detects any errors in the parameters. This will cause a derivation error in the type.

Examples of generated code

If call is set:

{
call: scriptNode
}

where scriptNode is mycompany.products.myproduct.library.SomeScript, then the generated code will be:

if ( !application.errorFound() ) {
application.put('contextMemberType',application.getNode('contextMemberTypeReference'));
application.call('mycompany.product.myproduct.library.SomeScript');
}

contextMemberTypeReference will be set to the reference of the context member type passed to modifyScript().

If script is set:

{
script: "application.sysout('Hello World');\n"
}

Then the generated code will be:

if ( !application.errorFound() ) {
(function(contextMemberType) {
application.sysout('Hello World');
})(application.getNode('contextMemberTypeReference'));
}

If fn is set:

{
fn: function(contextMemberType) {
application.deleteAll(application.getContext(),contextMemberType);
}
}

Then the generated code will be:

if ( !application.errorFound() ) {
(function(contextMemberType) {
application.deleteAll(application.getContext(),contextMemberType);
})(application.getNode('contextMemberTypeReference'));
}

Note that "application.getContext()" will be evaluated from within the type's method, not in the member type's code.

Generally:

  • Use the script options for scripts that need to be generated, for example because they contain node references.
  • Use the call option for significant and complicated code that may need to be reused in other places.
  • Use fn in other cases.
ModifyType.setProperty(property,value,append)

Set or overwrite the given property.

If append is false (the default), then if the property already exists it is overwritten.

If append is true, then if the property already exists append the value to it after a newline.

ModifyType.deleteMemberType(memberType);

Delete a member type from the type. memberType defaults to the context member type.

Tag List