-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jack Ellis
authored and
Jack Ellis
committed
Mar 25, 2017
1 parent
8fc9f9b
commit 664ce36
Showing
34 changed files
with
136 additions
and
1,317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,146 @@ | ||
JPEX - Javascipt Protoype Extension | ||
=================================== | ||
[](https://travis-ci.org/jackmellis/jpex) | ||
|
||
 | ||
=========== | ||
Easy Dependency Injection | ||
-------------------------- | ||
|
||
[](https://travis-ci.org/jackmellis/jpex) | ||
[](https://badge.fury.io/js/jpex) | ||
[](https://codeclimate.com/github/jackmellis/jpex) | ||
[](https://codeclimate.com/github/jackmellis/jpex/coverage) | ||
|
||
Jpex is a Class wrapper and Inversion of Control framework. | ||
|
||
Jpex makes it easy to create an Object-Oriented-style application in Node. It wraps up a lot of the prototypical quirks of Javascript so you can focus on creating purposeful classes. This is an intentional move away from static functions and modules in favour of providing all complex objects in the form of class instances. | ||
*This the 2.0 of Jpex. For version 1, please refer to the [v1](https://github.com/jpex-js/jpex/blob/1.x/docs/index.md) docs* | ||
|
||
The second purpose of Jpex is to make dependency injection extremely easy. You shouldn't ever have to `require` a module within your application (other than Jpex itself), but you also shouldn't have to worry about manually injecting dependencies into every class. | ||
Nobody wants to call a function with 20 parameters in a specific order. | ||
It's not readable, maintainable, or fun. Proper dependency injection should be automagical and the caller shouldn't care what the callee depends on. | ||
## Getting Started | ||
|
||
Usage | ||
----- | ||
Easy: require, extend, and instantiate! | ||
### Install | ||
Jpex is available on [npm](https://www.npmjs.com/package/jpex): | ||
`npm install jpex --save` | ||
|
||
You can then include Jpex in your commonJs project: | ||
```javascript | ||
var jpex = require('jpex'); | ||
var Jpex = require('jpex'); | ||
Jpex.extend(...); | ||
``` | ||
Which works out-of-the-box with *node*, *webpack* and *browserify* | ||
|
||
Jpex also comes with a pre-built js file which can be included as a script tag on your page: | ||
``` | ||
<script src="node_modules/jpex/dist/jpex.js"></script> | ||
<script> | ||
Jpex.extend(...); | ||
</script> | ||
``` | ||
|
||
var MyClass = jpex.extend(function($log){ | ||
this.thing = 'hello'; | ||
$log('My Class instantiated'); | ||
You can also download the source code from [github](https://github.com/jpex-js/jpex) | ||
|
||
------ | ||
|
||
### Registering Services | ||
Services and factories are small modules or functions that provide a reusable or common piece of functionality. In Jpex, you can register **factories**: | ||
```javascript | ||
Jpex.register.factory('myFactory', function(){ | ||
return {}; | ||
}); | ||
``` | ||
**services**: | ||
```javascript | ||
Jpex.register.service('myService', function(){ | ||
this.method = function(){ | ||
... | ||
}; | ||
}); | ||
``` | ||
and **constants**: | ||
```javascript | ||
Jpex.register.constant('myConstant', 'foo'); | ||
``` | ||
|
||
------ | ||
|
||
### Using Factories | ||
Once registered, you can request any factory when creating a new Jpex class: | ||
```javascript | ||
var Class = Jpex.extend(function(myFactory, myService, myConstant){ | ||
myService.method(); | ||
myConstant === 'foo'; | ||
}); | ||
|
||
new Class(); // creates an instance of Class using the above constructor function. | ||
``` | ||
|
||
var instance = new MyClass(); | ||
You can also request a factory from Jpex directly: | ||
```javascript | ||
var myService = Jpex.$resolve('myService'); | ||
var myConstant = Jpex.$resolve('myConstant'); | ||
|
||
myService.method(); | ||
myConstant === 'foo'; | ||
``` | ||
There is much more that can be done with Jpex. Checkout the full documentation at https://github.com/jackmellis/jpex/blob/master/docs/index.md | ||
|
||
And finally, you can request a dependency from within another factory: | ||
```javascript | ||
Jpex.register.constant('myConstant', 'foo'); | ||
|
||
Jpex.register.factory('myFactory', function(myConstant){ | ||
return { | ||
injectedValue : myConstant | ||
}; | ||
}); | ||
|
||
Jpex.register.service('myService', function(myFactory){ | ||
this.method = function(){ | ||
return myFactory.injectedValue; | ||
}; | ||
}); | ||
|
||
Jpex.$resolve('myService').method(); // returns 'foo'! | ||
``` | ||
|
||
------- | ||
|
||
### Extend & Instantiate | ||
You can `extend` Jpex to create a whole new *class* with its own constructor and factories. You can also extend an extended class and so on. Each extended class inherits its parent's properties and factories. | ||
```javascript | ||
var Class = Jpex.extend(function(inheritedService){ | ||
// ... | ||
}); | ||
|
||
var SubClass = Class.extend({ | ||
constructor : function(inheritedService){ | ||
... | ||
}, | ||
invokeParent : true, // will fire off the parent constructor | ||
methods : { | ||
methodA : function(){ | ||
// ... | ||
} | ||
} | ||
}); | ||
``` | ||
you can invoke a Jpex class with the `new` keyword. This will create a new instance of the class: | ||
```javascript | ||
var instance = new SubClass(); | ||
|
||
instance.methodA(); | ||
``` | ||
|
||
------ | ||
|
||
### Plugins | ||
There are several [plugins](/plugins) available that add extra functionality to Jpex. The most fundamental are [jpex-node](https://www.npmjs.com/package/jpex-node) and [jpex-web](https://www.npmjs.com/package/jpex-web) that add some useful default factories to your node or web application: | ||
```javascript | ||
Jpex.use(require('jpex-node')); | ||
Jpex.$resolve('$fs'); | ||
Jpex.$resolve('$promise'); | ||
|
||
// or | ||
Jpex.use(require('jpex-web')); | ||
Jpex.$resolve('$window'); | ||
|
||
// etc. | ||
``` | ||
|
||
For more information, see the full documentation at [https://jpex-js.github.io](https://jpex-js.github.io) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.