Comparing version 2.0.0-alpha.3 to 2.0.0
{ | ||
"name": "jpex", | ||
"version": "2.0.0-alpha.3", | ||
"version": "2.0.0", | ||
"description": "Javascript Prototype Extension", | ||
@@ -29,3 +29,3 @@ "main": "src/index.js", | ||
}, | ||
"homepage": "https://github.com/jackmellis/jpex#readme", | ||
"homepage": "https://jpex-js.github.io", | ||
"devDependencies": { | ||
@@ -32,0 +32,0 @@ "eslint": "^3.15.0", |
149
README.md
@@ -1,4 +0,8 @@ | ||
JPEX - Javascipt Protoype Extension | ||
=================================== | ||
[![Build Status](https://travis-ci.org/jackmellis/jpex.svg?branch=master)](https://travis-ci.org/jackmellis/jpex) | ||
![Jpex](https://jpex-js.github.io/dist/jpex.svg) | ||
=========== | ||
Easy Dependency Injection | ||
-------------------------- | ||
[![Build Status](https://travis-ci.org/jpex-js/jpex.svg?branch=master)](https://travis-ci.org/jackmellis/jpex) | ||
[![npm version](https://badge.fury.io/js/jpex.svg)](https://badge.fury.io/js/jpex) | ||
@@ -10,21 +14,134 @@ [![Code Climate](https://codeclimate.com/github/jackmellis/jpex/badges/gpa.svg)](https://codeclimate.com/github/jackmellis/jpex) | ||
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* | ||
var MyClass = jpex.extend(function($log){ | ||
this.thing = 'hello'; | ||
$log('My Class instantiated'); | ||
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> | ||
``` | ||
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'); | ||
``` | ||
var instance = new MyClass(); | ||
------ | ||
### 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. | ||
``` | ||
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 | ||
You can also request a factory from Jpex directly: | ||
```javascript | ||
var myService = Jpex.$resolve('myService'); | ||
var myConstant = Jpex.$resolve('myConstant'); | ||
myService.method(); | ||
myConstant === 'foo'; | ||
``` | ||
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) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
76463
27
0
147