Skip to content

Commit bb14707

Browse files
committed
Update the facade chapter
1 parent b49afdc commit bb14707

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,25 +447,50 @@ Using this pattern is especially useful when we need to modify the functionality
447447
448448
![Facade](./images/facade.png "Fig. 11")
449449

450-
There are a few facades in AngularJS. Each time you want to provide higher level API to given functionality you usually use facade.
450+
There are a few facades in AngularJS. Each time you want to provide higher level API to given functionality you practically create a facade.
451451

452-
For example, lets take a look at the `$http` service. We can use `$http` as a method, which accepts a configuration object:
452+
For example, lets take a look how we can create an `XMLHttpRequest` POST request:
453+
454+
```JavaScript
455+
var http = new XMLHttpRequest(),
456+
url = '/example/new',
457+
params = encodeURIComponent(data);
458+
http.open("POST", url, true);
459+
460+
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
461+
http.setRequestHeader("Content-length", params.length);
462+
http.setRequestHeader("Connection", "close");
463+
464+
http.onreadystatechange = function () {
465+
if(http.readyState == 4 && http.status == 200) {
466+
alert(http.responseText);
467+
}
468+
}
469+
http.send(params);
470+
```
471+
But if we want to post this data using the AngularJS' `$http` service we can:
453472

454473
```JavaScript
455474
$http({
456-
method: 'GET',
457-
url: '/someUrl',
458-
timeout: 1000
475+
method: 'POST',
476+
url: '/example/new',
477+
data: data
478+
})
479+
.then(function (response) {
480+
alert(response);
459481
});
460482
```
461-
Another way to use `$http` is by calling its methods:
483+
or we can even:
462484

463485
```JavaScript
464-
$http.get('/someUrl');
486+
$http.post('/someUrl', data)
487+
.then(function (response) {
488+
alert(response);
489+
});
465490
```
466-
The second option provides pre-configured version, which creates a HTTP GET request to the given URL.
491+
The second option provides pre-configured version, which creates a HTTP POST request to the given URL.
467492

468-
Even higher level of abstraction is being created by `$resource`, which is build over the `$http` service.
493+
Even higher level of abstraction is being created by `$resource`, which is build over the `$http` service. We will take a further look at this service in [Active Record](#active-record) and [Proxy](#proxy) sections.
469494

470495
#### Proxy
471496

0 commit comments

Comments
 (0)