Skip to content

Commit bc0ac69

Browse files
committed
Add components and services section
1 parent 0e561d7 commit bc0ac69

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,58 @@ function MyCtrl(Developer) {
272272

273273
## Angular 2 Overview
274274

275+
Angular 2 is the latest version of the AngularJS framework and it in moment of writing this document its design and development are still in progress. The entire structure of the framework and all the components are reconsidered so there are a lot of changes. Angular 2 is developed in TypeScript it contains minimal set of components compared to AngularJS 1.x so some people think of it more like a library rather than a framework. In the following sections we are going to describe most of these components. Since complete introduction to the framework is out of the scope of this paper, in the resource section there are links to getting started documents and blog posts.
276+
277+
### Components
278+
279+
The main building blocks of all Angular 2 applications are the components. We can think of the Angular 2 components as directives, which render the UI based on some state, stored inside them. This idea might seems quite familiar from ReactJS and basically it is. The main differences between the Angular 2 components and ReactJS are the mechanisms the data-binding is being implemented and the template processing. In Angular 2 the templates are consisted of actual markup, which is being inserted into the DOM and processed recursively based on other components found. However, in ReactJS we define our component using language called JSX, which is later transpiled to explicit JavaScript method calls.
280+
281+
The business logic of your application should be isolated in separate services, outside the actual components.
282+
283+
```javascript
284+
// example components
285+
```
286+
287+
### Services
288+
289+
Similarly to AngularJS 1.x, Angular 2 has services, however the syntax for defining them is completely different. In Angular 2 the services are simply TypeScript classes, which could be injected inside other services or components using the dependency injection mechanism of Angular 2. Responsibilities to the services in Angular 2 are to encapsulate the business logic, implement the interaction with remote services and create injectable adapters to browser APIs (for example `localStorage`).
290+
291+
Here is an example of a service, which defines store of users:
292+
293+
```javascript
294+
class UsersList {
295+
users:List<Users>;
296+
constructor() {
297+
this.users = new List<User>();
298+
}
299+
add(user:User) {
300+
this.users.push(User);
301+
}
302+
remote(user:User) {
303+
this.users.remove(user);
304+
}
305+
fetchAll() {
306+
let xhr = new XHR();
307+
return xhr.get('/users')
308+
.then((users) => {
309+
users.forEach((u) => {
310+
this.users.add(new User(u));
311+
});
312+
return this.users;
313+
});
314+
}
315+
fetch(id:Number) {
316+
let xhr = new XHR();
317+
return xhr.get(`/users/${id}`)
318+
.then((user) => {
319+
let u = new User(user);
320+
this.users.add(u);
321+
return u;
322+
});
323+
}
324+
}
325+
```
326+
275327
## AngularJS in Patterns
276328

277329
In the next a couple of sections, we are going to take a look at how the traditional design and architectural patterns are composed in the AngularJS components. The following chapter is devided in two parts - AngularJS 1.x and Angular 2. In the corresponding chapters the patterns will be put in the appropriate context.

0 commit comments

Comments
 (0)