You can use services to organize and share code across your app.
Noder.io services are:
- Lazily loaded: Noder.io only calls a service when another item depends on it.
- Singletons: Each item dependent on a service gets a reference to the single returned value generated by the service at the first call.
- in the IoC container: like all other values, all services are stored in the IoC container (see collection) and accessible via dependency injection (DI).
Creating Services
Each services can be created via the collection noder.$di and the shortcuts of the methods factories (services constructors):
- $singleton
- $provider
- $factory
Each services is accessible via the collection noder.$di and the shortcuts of the methods that supports dependency injection:
- $apply
- $inject
- $invoke
- $provider
- $factory
Registering a service with $singleton
The method $singleton create a simple service without dependencies.
noder.$singleton('serviceId', function() {
var myService;
// function body that constructs myService
return myService;
});
Registering a service with $provider
The method $provider create a service that supports dependencies injection.
The raw value of each dependency is passed to the factory function that constructs the service.
It's the same type of injection that $inject(['dep1', 'dep2'], fn)
.
noder.$provider('serviceId', ['Dep1', 'dep2'], function(SomeClass, options) {
var myService;
// factory function body that constructs myService
myService = new SomeClass(options);
// ...
return myService;
});
When the service serviceId
is called, Noder.io calls the factory function (service constructor) by passing dependencies ['Dep1', 'dep2']
, then the object myService
is returned.
On subsequent calls of the service serviceId
, the reference of myService
object will be returned directly (singleton).
Registering a service with $factory
The method $factory create a service that supports dependencies injection.
The returned value of each dependency is passed to the factory function that constructs the service.
It's the same type of injection that $invoke(['dep1', 'dep2'], fn)
.
noder.$factory('serviceId', ['dep1', 'dep2'], function(ajax, url) {
var myService;
// factory function body that constructs myService
var contents = ajax.get(url);
// ...
return myService;
});
When the service serviceId
is called, Noder.io calls the factory function (service constructor) by passing dependencies ['dep1', 'dep2']
, then the object myService
is returned.
If a dependency is a function
, it is invoked and the returned value is passed in argument to the factory function.
On subsequent calls of the service serviceId
, the reference of myService
object will be returned directly (singleton).
Using services
With $di.get():
var service = noder.$di.get('serviceId');
With $apply:
var service = noder.$apply(function() {
return this.serviceId;
});
With $inject:
var myVar = noder.$inject(['someDep', 'another'], function(someDep, another) {
return this.serviceId.anyMethod(someDep, another);
});
And with all other dependencies injectors like $provider, $factory, $invoke, ...