mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-01 10:30:51 -05:00
updated plugin examples
parent
3d2d6d0b32
commit
3788850f59
1 changed files with 11 additions and 7 deletions
|
@ -112,21 +112,24 @@ var MyPlugin = {
|
||||||
install: function(base) {
|
install: function(base) {
|
||||||
base.Engine.create = Matter.chain(
|
base.Engine.create = Matter.chain(
|
||||||
base.Engine.create,
|
base.Engine.create,
|
||||||
MyPlugin.create
|
function() {
|
||||||
|
MyPlugin.Engine.init(this);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
create: function(engine) {
|
Engine: {
|
||||||
engine = this || engine;
|
init: function(engine) {
|
||||||
console.log('Engine created:', engine);
|
// do something with engine
|
||||||
|
console.log('MyPlugin.Engine.init:', engine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
When this plugin is installed, it will log to the console `Engine created: ...` whenever `Matter.Engine.create` is called.
|
When this plugin is installed, it will log to the console `'MyPlugin.Engine.init: ...` whenever `Matter.Engine.create` is called.
|
||||||
The line `engine = this || engine` allows the `create` function to be used outside of a `Matter.chain` if desired.
|
|
||||||
|
|
||||||
Note that by using `Matter.chain` you can also:
|
Note that by using `Matter.chain` you can also:
|
||||||
- chain before _or_ after the original method
|
- chain before _or_ after the original method
|
||||||
|
@ -171,12 +174,13 @@ Some general guidelines for plugins:
|
||||||
- document everything (code and readme)
|
- document everything (code and readme)
|
||||||
- build plugins with sharing and reuse in mind
|
- build plugins with sharing and reuse in mind
|
||||||
- don't _add_ new functions to modules or namespaces you don't maintain (only patch existing functions)
|
- don't _add_ new functions to modules or namespaces you don't maintain (only patch existing functions)
|
||||||
|
- follow the same namespacing structure as the core (e.g. `MyPlugin.Body.init`, `MyPlugin.Engine.update`)
|
||||||
- expose and implement your plugin's functions so that they can be called manually
|
- expose and implement your plugin's functions so that they can be called manually
|
||||||
- avoid relying a particular order of execution where possible
|
- avoid relying a particular order of execution where possible
|
||||||
- the smaller the better
|
- the smaller the better
|
||||||
- but avoid unnecessary dependencies
|
- but avoid unnecessary dependencies
|
||||||
- don't modify options or settings in unexpected or undocumented ways
|
- don't modify options or settings in unexpected or undocumented ways
|
||||||
- check it before you wreck it (use conditionals)
|
- use conditionals before operating on possibly undefined properties
|
||||||
- don't bundle dependencies (document them)
|
- don't bundle dependencies (document them)
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
Loading…
Reference in a new issue