Skip to content

Commit 2a8a054

Browse files
committed
Basically completed the guide of 06
1 parent e004e8a commit 2a8a054

File tree

2 files changed

+86
-29
lines changed

2 files changed

+86
-29
lines changed

06 - Type Ahead/README.md

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313

1414
## 涉及特性
1515

16-
- `promise`
16+
- Promise
1717
- `fetch()`
1818
- `then()`
1919
- `json()`
2020
- Array
2121
- `filter()`
22+
- `map()`
2223
- `push()`
2324
- `join()`
24-
- `RegExp`
25+
- Spread syntax 扩展语句
26+
- RegExp
2527
- `match()`
2628
- `replace()`
2729

2830
## 过程指南
2931

3032
1. 声明一个空数组,用于存放解析 json 后的数据
31-
2. 运用 `fetch()` 获取 json 文件
32-
1. 获取 json 响应
33-
2. 解析数据
33+
2. 运用 `fetch()` 发送 HTTP 请求
34+
1. 获取返回的 Promise 对象
35+
2. 解析 JSON 数据
3436
3. 存入数组
3537
3. 获取两个主要 HTML 元素(`<input>``<ul>`),给 `<input>` 添加事件监听(`change`, `keyup`
3638
4. 编写匹配输入的函数
@@ -50,23 +52,88 @@ Fetch API 这个新特性,是 XMLHttpRequest 获取资源新的替代方案,
5052

5153
#### [fetch()](https://developer.mozilla.org/zh-CN/docs/Web/API/GlobalFetch/fetch)
5254

53-
Fetch API 提供一个全局的方法 `fetch()`,这个方法(至少)需要接受 `资源的路径` 作为参数,返回值是一个 Promise 对象。若请求成功,这个对象包含了 Request 对应的 Response,但这只是一个 HTTP 相应
55+
Fetch API 提供一个全局的方法 `fetch()`,这个方法(至少)需要接受 `资源的路径` 作为参数,返回值是一个 Promise 对象。若请求成功,这个对象包含了(对应 Request 的)Response,但这只是一个 HTTP 响应
5456

5557
语法如下:
5658

5759
```js
5860
fetch(input, init).then(function(response) { ... });
5961
```
6062

61-
####
63+
MDN 中有个[发送基本的 fetch 请求的示例](https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch#发起_fetch_请求)如下:
6264

63-
在这个挑战的示例中,使用此方法的截图如下:
65+
```js
66+
var myImage = document.querySelector('img');
67+
68+
fetch('flowers.jpg')
69+
.then(function(response) {
70+
return response.blob();
71+
})
72+
.then(function(myBlob) {
73+
var objectURL = URL.createObjectURL(myBlob);
74+
myImage.src = objectURL;
75+
});
76+
```
6477

65-
![fetch(url).then()](https://cl.ly/3P3F1F2y1510/Image%202017-01-01%20at%206.58.45%20PM.png)
78+
用 ES6 的语法来写就是这样:
79+
80+
```js
81+
const myImage = document.querySelector('img');
82+
83+
fetch('flowers.jpg')
84+
.then(response => response.blob())
85+
.then(myBlob => {
86+
const objectURL = URL.createObjectURL(myBlob);
87+
myImage.src = objectURL;
88+
});
89+
```
90+
91+
这个示例中使用了 `blob()` 方法来获取图片的内容,这是 Body 类定义的一个方法,除此之外还有可以获取其他内容的方法,可以[在这里看](https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch#Body),也可以在 Console 面板中查看:
92+
93+
![Body 类的方法](https://cl.ly/143N2R1b3T1o/Image%202017-01-03%20at%209.15.37%20AM.png)
94+
95+
在这个挑战中,我们主要是利用 `.json()`,以使用 JSON 对象来读取 Response 流中的数据,读取之后,Body 的 body.Uesd 属性值会变为已读。另外较为常用的方法还有:`blob()``text()``arrayBuffer()``formData()`
96+
97+
### [ES6 中的数组扩展语法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)
98+
99+
利用扩展运算符可以[替代 ES5 中的 `push` 方法添加一个数组到另一个数组末尾](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_operator#更好的_push_方法),两种语法的写法如下:
66100

101+
```js
102+
// 将arr2中的所有元素添加到arr1中
103+
104+
// ES5
105+
var arr1 = [0, 1, 2];
106+
var arr2 = [3, 4, 5];
107+
Array.prototype.push.apply(arr1, arr2);
108+
109+
// ES6
110+
var arr1 = [0, 1, 2];
111+
var arr2 = [3, 4, 5];
112+
arr1.push(...arr2);
113+
```
114+
115+
### 此挑战中如何异步获取数据并解析
116+
117+
这一部分用到的语句很简单,但都用了 ES6 的语法,所以不熟悉 ES6 的话需要时间适应一下。
118+
119+
````js
120+
const endpoint = 'https://raw.githubusercontent.com/soyaine/FE-Practice/f438d3bdf099461f88322b1b1f20c9d58f66f1ec/TangPoetryCut.json';
121+
const poetrys = [];
122+
fetch(endpoint)
123+
.then(blob => blob.json())
124+
.then(data => poetrys.push(...data));
125+
````
126+
127+
如果把每条语句分解开来运行,得到的返回结果可以看下面的截图。
128+
129+
![fetch(url).then()](https://cl.ly/3P3F1F2y1510/Image%202017-01-01%20at%206.58.45%20PM.png)
67130

131+
### 正则表达式
68132

133+
获取到了数据之后,如何匹配输入值呢?就要利用正则表达式了。正则表达式的 `match()` 可以执行一个匹配操作,我们再结合 `Array.filter()` 便能筛出整个数组中,满足条件的项,再经过字符串处理即可输出到页面。
69134

135+
> 这篇我写了很久也写不满意,如果你能读到这里,说明你对实现的效果有兴趣,如果你觉得有什么地方我写得不清楚,或者我遗漏了什么,请告诉我。我一直在思考和调整,用什么样的方式去写会比较容易看懂,万分期待和感恩能有读者反馈 soyaine1@gmail。
70136
71137

72-
> 10:45 -
138+
> 创建时间:2016-12-31
139+
> 最后更新:2017-01-03

06 - Type Ahead/index-SOYAINE.html

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,32 @@
1717
<script>
1818
// const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
1919

20-
const endpoint = 'https://raw.githubusercontent.com/soyaine/FE-Practice/f438d3bdf099461f88322b1b1f20c9d58f66f1ec/TangPoetryCut.json';
20+
const endpoint = 'https://raw.githubusercontent.com/soyaine/FE-Practice/f438d3bdf099461f88322b1b1f20c9d58f66f1ec/TangPoetryCut.json';
2121

2222

23-
const poetrys = [];
24-
fetch(endpoint)
23+
const poetrys = [];
24+
fetch(endpoint)
2525
.then(blob => blob.json())
2626
.then(data => poetrys.push(...data));
2727

2828
function findMatches( wordToMatch, poetrys) {
2929
return poetrys.filter(poet => {
30-
// 找出匹配的诗句
30+
// 正则找出匹配的诗句
3131
const regex = new RegExp(wordToMatch, 'gi');
32-
let author = poet.detail_author.join('');
32+
const author = poet.detail_author.join('');
3333
// console.log(author);
3434
return poet.detail_text.match(regex) || poet.title.match(regex) || author.match(regex);
3535
});
3636
}
37-
38-
// function displayMatches() {
39-
// const matches = findMatches(this.value, poetrys);
40-
// suggestions.innerHTML = "";
41-
// matches.forEach(poet => {
42-
// const li = document.createElement('li');
43-
// li.textContent = poet.detail_text;
44-
//// .replace('。', '。'+'&#8291;');
45-
// suggestions.appendChild(li);
46-
// console.log(poet.detail_text)
47-
// });
48-
// }
49-
37+
5038
function displayMatches() {
5139
const matches = findMatches(this.value, poetrys);
5240
const regex = new RegExp(this.value, 'gi');
5341
const html = matches.map( poet => {
42+
// 替换高亮的标签
5443
const text = poet.detail_text.replace(regex, `<span class="hl">${ this.value }</span>`);
5544
const title = poet.title.replace(regex, `<span class="hl">${ this.value }</span>`)
45+
// 构造 HTML 值
5646
return `
5747
<li>
5848
<span class="poet">${ text }</span>
@@ -70,7 +60,7 @@
7060
search.addEventListener('change', displayMatches);
7161
search.addEventListener('keyup', displayMatches);
7262

73-
// console.log(cities);
63+
// console.log(poetrys);
7464
</script>
7565
</body>
7666
</html>

0 commit comments

Comments
 (0)