上药三品,神与气精

曾因酒醉鞭名马 生怕情多累美人


  • 首页

  • 关于

  • 分类

  • 标签

  • 归档

  • 搜索

js0026

发表于 2018-12-21 | 阅读次数:
字数统计: 0 | 阅读时长 ≈ 1

js0025

发表于 2018-12-21 | 阅读次数:
字数统计: 0 | 阅读时长 ≈ 1

js0024

发表于 2018-12-21 | 阅读次数:
字数统计: 222 | 阅读时长 ≈ 1

生成器和promise 的结合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html>
<head>
<title>Combining generators and promises</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../assert.css">
<script src="../assert.js"></script>
<script src="getJSON.js"></script>
<script>
"use strict"
async(function*(){
try {
const ninjas = yield getJSON("data/ninjas.json");
const missions = yield getJSON(ninjas[0].missionsUrl);
const missionDescription = yield getJSON(missions[0].detailsUrl);

assert(ninjas !== null && missions !== null
&& missionDescription !== null,
"All ready!");
}
catch(e) {
fail("We weren't able to get mission details");
}
});

function async(generator) {
const iterator = generator();

function handle(iteratorResult) {
if(iteratorResult.done) { return; }

const iteratorValue = iteratorResult.value;

if(iteratorValue instanceof Promise) {
iteratorValue.then(res => handle(iterator.next(res)))
.catch(err => iterator.throw(err))
}
}

try {
handle(iterator.next());
}
catch (e) { iterator.throw(e); }
}
</script>
</head>
<body>Has to be run on a server (e.g MAMP or WAMP)</body>
</html>

js0023

发表于 2018-12-21 | 阅读次数:
字数统计: 197 | 阅读时长 ≈ 1

创建真实的promise 案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<head>
<title>Creating a JSON promise</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../assert.css">
<script src="../assert.js"></script>
<script>
function getJSON(url) {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url);

request.onload = function() {
try {
if(this.status === 200 ){
resolve(JSON.parse(this.response));
} else{
reject(this.status + " " + this.statusText);
}
} catch(e){
reject(e.message);
}
};

request.onerror = function() {
reject(this.status + " " + this.statusText);
};

request.send();
});
}

getJSON("data/ninjas.json").then((ninjas) => {
assert(ninjas !== null, "Ninjas obtained!");
}).catch(e => fail("Shouldn’t be here:" + e));
</script>
</head>
<body>
<p>Has to be executed on a server (e.g MAMP, WAMP)</p>
</body>
</html>

django-template

发表于 2018-12-21 | 阅读次数:
字数统计: 90 | 阅读时长 ≈ 1

有些情况会自定义tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#!/usr/bin/env python
#coding:utf-8


from django import template
from django.utils.safestring import mark_safe


register = template.Library()


@register.simple_tag
def my_simple_time(v1,v2,v3):
return v1 + v2 + v3

@register.simple_tag
def my_input(id,arg):
result = "<input type='text' id='%s' class='%s' />" %(id,arg,)
return mark_safe(result)

如何使用

1
2
3
4
{% load xx %}

{% my_simple_time 1 2 3%}
{% my_input 'id_username' 'hide'%}
1…596061…109
John Cheung

John Cheung

improve your python skills

543 日志
33 分类
45 标签
RSS
GitHub Email
© 2020 John Cheung
本站访客数:
|
主题 — NexT.Pisces v5.1.4
博客全站共226.3k字