The application.require() method runs code in another script, and returns the value of the "exports" attribute.
This can be used to create classes which represent behaviours of node types and instances which represent nodes.
Create a class
Create a class which defines the behaviour of the node, and hold this in a separate script node.
MyClass = function(context) {
this.context = context;
}
MyClass.prototype.getName = function() {
return this.context.getNodeName();
}
MyClass.prototype.runRequest = function() {
application.put('return','In MyClass.runRequest()');
}
application.put('exports',MyClass);
The important things are that the class has a constructor which is passed a context node and that the exports attribute is set to the class.
The methods in this class - getName() and runRequest() - are just examples.
The previous values of the "exports" attribute is preserved through a require(), so using require() within a require() works.
Adding class support to the node type
On the node type, set the binding "scriptClass" to the node that holds your script.
In your member type list, add the Derive node instance member type. This will add script support to instances of the node type.
Using the instance
You can access an instance simply by requiring the node.
var instance = application.require(node);
Service execution
It is easy to support service execution processing within a node class.
Create a method to respond to the request (runRequest() in the example above).
In the Script member of the node type, add something like this:
application.require(application.getContext()).runRequest();