-
Notifications
You must be signed in to change notification settings - Fork 3
Plugin Authoring
In ClassicalJS, plugins have the power to do even more than what the user of the library can do.
They can add methods and properties, modify them, modify their configuration and have access to internal objects that the users don't even have access to.
To add a plugin to the library, use Class.addPlugin
.
You need to pass an object to Class.addPlugin
describing your plugin. Here is what that object should contain:
###>name
The name of your plugin. It's what the user would use as a name in ._()
, and what ClassicalJS uses to refer to your plugin internally.
###>chainName
(Default: name
)
The chainName
is the name of the function that the user would use if he wants to apply your plugin without a ._()
call. It defaults to name
, but if you want to set it to something different, you can.
Private("firstName");
CustomChainName(); //the plugin's name can be something different
###>position
The position
can have the value of "before"
or "after"
.
It specifies whether your plugin should be written before or after a property/method declaration.
So if it's "before"
, the user would do:
YourPlugin();
Private("firstName");
And if it's "after"
, the user would do:
Private("firstName");
YourPlugin();
position
could also be "void"
, which means that your plugin is not applied to any property or method, but an empty command is created where your plugin is applied you (this allows you to add functionalities to the whole Class, instance of a property or method)
###>priority
The priority
specifies when your plugin should be executed compared to other plugins. It should be a number or one of these strings:
-
"high"
: which is equivalent to 10 -
"medium"
: which is equivalent to 20 -
"low"
: which is equivalent to 30
The plugins are ordered in ascending order depending on their priority, and are executed in that order.
Void plugins (plugins with a position
set to void) automatically get a priority of -10000, to make sure they execute before any other plugin on their command.
###>onDefinition
When a Class has finished being defined, ClassicalJS will iterate through all the Class's commands, go through each command's plugin list, and try to execute the onDefinition
function of each plugin.
The first argument passed to onDefinition
is an info Object, describing on which property/method the plugin had been applied on.
The info object will contain:
-
command
: an object describing the command (see below) -
Class
: the instance of Class that the command is attached to.
The rest of the arguments passed to onDefinition
will be the arguments that the user has passed while he was applying your plugin.
You can use onDefinition
to change the command (it's value or it's name), to add methods and properties to the Class or even to modify other commands.
###>onInstanceCreation
When a new instance of a Class is being created, ClassicalJS will iterate through all the Class's commands, go through each command's plugin list, and try to execute the onInstanceCreation
function of each plugin.
The first argument passed to onDefinition
is an info Object, describing on which property/method/instance the plugin had been applied on.
The info object will contain:
-
command
: an object describing the command (see below) -
Class
: the instance of Class that the command is attached to. -
privateObj
: the private layer of the instance. It's thethis
of the methods -
protectedObj
: the protected layer of the instance. It's the layer(object) that other Classes inherit -
publicObj
: the public layer of the instance. It's what anyone can access -
parentObj
: an object containing the parent's privateObj/protectedObj/publicObj -
pluginData
: an object shared by all the plugins that got applied on this instance. You can add to it, and other plugins can use what you've added
Only on methods commands:
-
fn
: a version of command.value, where it'sthis
is bound to privateObj
Only available on properties commands:
-
getterSetters
: an object that will be used to set the getters/setters of the property on the protectedObj and the publicObj. It has aget
property and aset
property, that are function that will become the getter/setter of the property. There is also asourceHasGetterSetter
property, which if set to true, will add the getter/setters to the privateObj (default to false), as well as asourceSetToInitialValue
property, which if set to false, won't explicitly set the property to it's default value on the privateObj.
This allows you to have full control over a property.
Only on Void plugin commands:
-
fn
andgetterSetters
are set to null, and the void plugin should populate them.
##Command Object (TODO: document)