react-services-injector
Advanced tools
Comparing version 0.0.3 to 0.1.0
46
index.js
@@ -0,1 +1,9 @@ | ||
let Service = function() { | ||
this.services = {}; | ||
this.servicesDidRegistered = () => { | ||
this.services = injector.get(); | ||
} | ||
}; | ||
class Injector { | ||
@@ -9,2 +17,3 @@ constructor() { | ||
createInstance(service) { | ||
let self = this; | ||
let instance = new service(); | ||
@@ -14,16 +23,16 @@ let methods = Object.getOwnPropertyNames(Object.getPrototypeOf(instance)); | ||
methods.forEach(method => { | ||
if (method === 'constructor' || method.indexOf('get') === 0) | ||
return; | ||
if (method === 'constructor' || method.indexOf('get') === 0 || method.indexOf('_') === 0) | ||
return; | ||
instance['__' + method] = instance[method]; | ||
instance['__' + method] = instance[method]; | ||
instance[method] = (...args) => { | ||
instance['__' + method].apply(instance, args); | ||
instance[method] = function (...args) { | ||
instance['__' + method].apply(instance, args); | ||
this.components.forEach(component => component.instance.forceUpdate.call(component.instance)); | ||
}; | ||
}); | ||
self.components.forEach(component => component.instance.forceUpdate.call(component.instance)); | ||
}; | ||
}); | ||
instance.$update = () => { | ||
this.components.forEach(component => component.instance.forceUpdate.call(component.instance)); | ||
instance.$update = function () { | ||
self.components.forEach(component => component.instance.forceUpdate.call(component.instance)); | ||
}; | ||
@@ -46,2 +55,10 @@ | ||
} | ||
this.services.forEach(service => service.instance.servicesDidRegistered.apply(service.instance)); | ||
this.services.forEach(service => { | ||
if (!service.instance.serviceDidConnected) | ||
return; | ||
service.instance.serviceDidConnected.apply(service.instance); | ||
}); | ||
} | ||
@@ -84,2 +101,6 @@ | ||
} | ||
static get name() { | ||
return component.name; | ||
} | ||
} | ||
@@ -94,5 +115,6 @@ | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = {injector}; | ||
module.exports = {injector, Service}; | ||
} else { | ||
exports.injector = injector; | ||
} | ||
exports.Service = Service; | ||
} |
{ | ||
"name": "react-services-injector", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "A library to create and use sigleton-services in your React application.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,3 +13,5 @@ React Services Injector | ||
```javascript | ||
export default class Storage { | ||
import {Service} from 'react-services-injector'; | ||
export default class Storage extends Service { | ||
constructor() { | ||
@@ -37,3 +39,3 @@ this.filter = ''; | ||
> **Important!** Methods that are not changing anything should be named as `getSomething`, starting with `get` keyword (don't get confused with getters). | ||
> **Important!** Methods that are not changing anything should be named as `getSomething`, starting with `get` keyword (don't get confused with getters). Also such methods may start from `_` character. | ||
@@ -102,5 +104,10 @@ Create an `index.js` in your `services` directory to export them all: | ||
``` | ||
> **Important!** You definitely shouldn't use services in constructor. You can't to, actually. Use it, for example, in the `componentWillMount` lifecycle method if you need something to be done once component is created. | ||
You can also use services in another services in the same way in any method except `constructor`. | ||
Initialization | ||
-------------- | ||
If you need to do some initialization of your service (probably asynchronous), you can use `serviceDidConnected` lifecycle method of service. That is the only lifecycle method so far. | ||
Behavior | ||
@@ -107,0 +114,0 @@ === |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88
132
8726
4