Skip to content

Commit

Permalink
fixed async
Browse files Browse the repository at this point in the history
  • Loading branch information
JayZangwill committed Apr 13, 2017
1 parent d6f0c62 commit f74bcb8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 50 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ modify lightings API, abolish some outdated configuration

### 2017 4.7 v2.0.1

optimized template rendering
optimized template rendering

### 2017 4.13 v2.0.2

1. Fixed issue where async could not be set to false
2. Fixed issue with false unable to get data when set to Async
55 changes: 34 additions & 21 deletions dist/lightings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Lightings v2.0.1
* Lightings v2.0.2
* Copyright (c) 2017 Jay Zangwill
*/
'use strict';
Expand Down Expand Up @@ -77,9 +77,10 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
return;
}
this.el = options && options.el;
//this.flag用于判断是不是xml

// this.flag用于判断是不是xml
this.flag = this.dataType = options && options.dataType || 'json';
this.async = options && options.async === false || true;
this.async = options && options.async === false ? false : true;
this.contentType = options && options.contentType || 'application/x-www-form-urlencoded; charset=UTF-8';
this.timeout = options && options.timeout || 0;
this.progress = options && options.progress;
Expand All @@ -92,29 +93,25 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
var timer = void 0;

return new Promise(function (resolve, reject) {
context.xhr.responseType = context.dataType.toLowerCase();
if (method === 'get') {
//如果是get请求,如果有数据,则吧数据添加在连接上发送到服务端
var url = context.url;
if (context.data) {
url += '?' + context.data;
}
context.xhr.open(method, url, context.async);
context.xhr.send(null);
} else if (method === 'post') {
//如果是post请求需要设置请求头,并且把数据作为send的参数
context.xhr.open(method, context.url, context.async);
context.xhr.setRequestHeader('Content-type', context.contentType);
context.xhr.send(context.data);

// 如果是同步请求就不能设置响应数据的类型
if (context.async) {
context.xhr.responseType = context.dataType.toLowerCase();
}
context.xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
if (context.flag === 'xml') {
resolve(this.responseXML);
} else if (context.isIe9) {
resolve(JSON.parse(this.responseText));
compile.call(context, JSON.parse(this.responseText));
} else if (context.isIe9 || !context.async) {

// 因为设置为同步不能设置响应数据的类型,所以要在这排错
try {
resolve(JSON.parse(this.responseText));
compile.call(context, JSON.parse(this.responseText));
} catch (e) {
resolve(this.responseText);
}
} else {
resolve(this.response);
compile.call(context, this.response);
Expand All @@ -127,6 +124,22 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
}
}
};
if (method === 'get') {

// 如果是get请求,如果有数据,则吧数据添加在连接上发送到服务端
var url = context.url;
if (context.data) {
url += '?' + context.data;
}
context.xhr.open(method, url, context.async);
context.xhr.send(null);
} else if (method === 'post') {

// 如果是post请求需要设置请求头,并且把数据作为send的参数
context.xhr.open(method, context.url, context.async);
context.xhr.setRequestHeader('Content-type', context.contentType);
context.xhr.send(context.data);
}
if (context.async && context.timeout > 0) {
timer = setTimeout(function () {
context.xhr.abort();
Expand All @@ -143,7 +156,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
function compile(data) {
var dom = document.querySelector(this.el),
key = void 0,
variable = data,
variable = void 0,
result = void 0,
childs = void 0,
text = void 0,
Expand Down
2 changes: 1 addition & 1 deletion dist/lightings.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions doc/README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@ lightings.get('test.json',{
### 2017 4.7 v2.0.1

优化模板渲染

### 2017 4.13 v2.0.2

1. 修正async无法设置为false的问题
2. 修正async设置为false时无法获取数据的问题
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lightings",
"version": "2.0.1",
"version": "2.0.2",
"description": "A lightweight Ajax Library",
"keywords": [
"lightweight",
Expand Down
55 changes: 34 additions & 21 deletions src/lightings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Lightings v2.0.1
* Lightings v2.0.2
* Copyright (c) 2017 Jay Zangwill
*/
'use strict';
Expand Down Expand Up @@ -75,9 +75,10 @@
return;
}
this.el = options && options.el;
//this.flag用于判断是不是xml

// this.flag用于判断是不是xml
this.flag = this.dataType = (options && options.dataType) || 'json';
this.async = (options && options.async === false) || true;
this.async = options && options.async === false ? false : true;
this.contentType = (options && options.contentType) || 'application/x-www-form-urlencoded; charset=UTF-8';
this.timeout = (options && options.timeout) || 0;
this.progress = options && options.progress;
Expand All @@ -90,29 +91,25 @@
let timer;

return new Promise((resolve, reject) => {
context.xhr.responseType = context.dataType.toLowerCase();
if(method === 'get') {
//如果是get请求,如果有数据,则吧数据添加在连接上发送到服务端
let url = context.url;
if(context.data) {
url += `?${context.data}`;
}
context.xhr.open(method, url, context.async);
context.xhr.send(null);
} else if(method === 'post') {
//如果是post请求需要设置请求头,并且把数据作为send的参数
context.xhr.open(method, context.url, context.async);
context.xhr.setRequestHeader('Content-type', context.contentType);
context.xhr.send(context.data);

// 如果是同步请求就不能设置响应数据的类型
if(context.async) {
context.xhr.responseType = context.dataType.toLowerCase();
}
context.xhr.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status >= 200 && this.status < 300 || this.status === 304) {
if(context.flag === 'xml') {
resolve(this.responseXML);
} else if(context.isIe9) {
resolve(JSON.parse(this.responseText));
compile.call(context, JSON.parse(this.responseText));
} else if(context.isIe9 || !context.async) {

// 因为设置为同步不能设置响应数据的类型,所以要在这排错
try{
resolve(JSON.parse(this.responseText));
compile.call(context, JSON.parse(this.responseText));
}catch(e){
resolve(this.responseText);
}
} else {
resolve(this.response);
compile.call(context, this.response);
Expand All @@ -125,6 +122,22 @@
}
}
}
if(method === 'get') {

// 如果是get请求,如果有数据,则吧数据添加在连接上发送到服务端
let url = context.url;
if(context.data) {
url += `?${context.data}`;
}
context.xhr.open(method, url, context.async);
context.xhr.send(null);
} else if(method === 'post') {

// 如果是post请求需要设置请求头,并且把数据作为send的参数
context.xhr.open(method, context.url, context.async);
context.xhr.setRequestHeader('Content-type', context.contentType);
context.xhr.send(context.data);
}
if(context.async && context.timeout > 0) {
timer = setTimeout(() => {
context.xhr.abort();
Expand All @@ -141,7 +154,7 @@
function compile(data) {
let dom = document.querySelector(this.el),
key,
variable = data,
variable,
result,
childs,
text,
Expand Down
11 changes: 6 additions & 5 deletions test/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
</head>

<body>
<div id="app">
<!--<div id="app">
{{author.firstName}}
<p>{{name}}</p>
{{ author.lastName }}
</div>
</div>-->
<script src="../dist/lightings.min.js"></script>
<script>
lightings.get('test.json',{
lightings.get('test.xml',{
config:true,
el:"#app"
el:"#app",
dataType:'xml'
}).then(function(data) {
console.log(data);
})
});
</script>
</body>

Expand Down

0 comments on commit f74bcb8

Please sign in to comment.