Skip to content

Latest commit

 

History

History
75 lines (53 loc) · 1.12 KB

di.md

File metadata and controls

75 lines (53 loc) · 1.12 KB

Dependency injection

All directives are created by slicky's dependency injection system. If you need to use service (model, etc) you should ask for it in constructor. You shouldn't care how to obtain access to some service, let someone else take care of it ;-)

Services

Users.ts:

import {Injectable} from 'slicky/di';

@Injectable()
export class Users
{

	public getUsers(fn: (users: Array<any>) => void): void
	{
		// ...
	}

}

bootstrap.ts:

// ...

import {Users} from './app/Users';

let container = new Container;

container.provide(Users);

// ...

UserComponent.ts:

import {Component} from 'slicky/core';
import {Users} from '../../Users';

@Component({
	selector: 'user',
})
export class UserComponent
{
	
	private users: Users;
	
	constructor(users: Users)
	{
		this.users = users;
	}
	
}

Factories

container.provide(Users, {
	useFactory: () => {
		return new Users;
	}
});

Services registered via useFactory option doesn't need to have @Injectable() annotation, so you can use this option to register libraries which are not compatible with slicky.