Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions 01 Non blocking/src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
document.onreadystatechange = () => {
const load = () => {
const search_button = document.getElementById('search_button');
const service = new apiIceAndFire.Service();
const printer = new printService.Printer();
const apiMapper = mapper.apiMapper();

search_button.addEventListener('click', (event) => {
event.stopPropagation();
const houseInput = document.getElementById('input-house');
const characterInput = document.getElementById('input-character');

if (houseInput.value && characterInput.value) {
console.log('1');
const housesRequestConfig = {
err: handleError,
callback: handleHousesRequestSucces(apiMapper, printer)
};
service.getHousesByName(houseInput.value, housesRequestConfig);
console.log('2');
const charactersRequestConfig = {
err: handleError,
callback: handleCharactersRequestSuccess(apiMapper, printer)
};
service.getCharactersByName(characterInput.value, charactersRequestConfig);
console.log('3');
getHouseBeforeCharacter(houseInput.value, characterInput.value);
} else {
alert('Introduce a values')
}
});
};

const service = new apiIceAndFire.Service();
const printer = new printService.Printer();
const apiMapper = mapper.apiMapper();

function getCharacter(character) {
const charactersRequestConfig = {
err: handleError,
callback: handleCharactersRequestSuccess(apiMapper, printer)
};
service.getCharactersByName(character, charactersRequestConfig);
}

function getHouseBeforeCharacter(house, character) {
const housesRequestConfig = {
err: handleError,
callback: handleHousesRequestSuccess(apiMapper, printer, character)
};
service.getHousesByName(house, housesRequestConfig);
}

function handleError() {
console.log(JSON.parse(this.responseText));
};

function handleHousesRequestSucces(apiMapper, printer) {
function handleHousesRequestSuccess(apiMapper, printer, character) {
return function () {
const data = JSON.parse(this.responseText);
const houses = apiMapper.housesMap(data);
printer.printHouses(houses, 'houses');
getCharacter(character);
};
};

function handleCharactersRequestSuccess(apiMapper, printer) {
return function () {
const data = JSON.parse(this.responseText);
const characters = apiMapper.caracthersMap(data);
const characters = apiMapper.charactersMap(data);
printer.printCharacters(characters, 'characters');
};
}
Expand Down
4 changes: 2 additions & 2 deletions 01 Non blocking/src/js/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var mapper = mapper || {};
}))
);

const caracthersMap = (characters) => (
const charactersMap = (characters) => (
characters.map(c => ({
name: c.name,
born: c.born,
Expand All @@ -21,7 +21,7 @@ var mapper = mapper || {};

return {
housesMap,
caracthersMap
charactersMap
}
};
})(mapper)
32 changes: 24 additions & 8 deletions 10 Event listeners are sync/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## In this demo we are going to work with Event Listeners.
# In this demo we are going to work with Event Listeners

* Folder structure:

```bash
10 Web Workers/
├── src/
│ ├── content/
Expand All @@ -10,15 +12,16 @@
│ ├── index.html
├── gulpfile.js
├── package.json
```

* Start from previous code demo.

## Steps.
## Steps

### 1. Refactor the `index.html` for our demo.
### 1. Refactor the `index.html` for our demo

```diff html
<!doctype html>
<!DOCTYPE html>

<html lang="en">
<head>
Expand All @@ -40,7 +43,7 @@

```

### 2. Lets change our `site.css`, for this demo.
### 2. Lets change our `site.css`, for this demo

```diff site.css
@import url('https://fonts.googleapis.com/css?family=Raleway');
Expand Down Expand Up @@ -91,9 +94,12 @@ body {
-}
-
```

### 3. Now we can add `main.js`

```javascript main.js
```javascript
main.js

(function () {
const btn = document.getElementById('btn');

Expand All @@ -113,8 +119,18 @@ body {
})();

```
### Challanege -> ¿Cuantos programas pequeños tenemos aquí? // 2
###¿Y cuando lo ejecutemos que se verá en la consola? // pre-click click-1 click-2 post-click

### Challenge -> ¿Cuántos programas pequeños tenemos aquí?

```javascript
// 2
```

### ¿Y cuándo lo ejecutemos que se verá en la consola?

```javascript
// pre-click click-1 click-2 post-click
```

* Lo normal es tender a pensar que hubiera 3 o 4 pequeños programas. Habría 4, si los EventListeners fueran disparados de manera asíncrona, pero los EventListeners son disparados de forma síncrona.

Expand Down