From f74bcb81ee9b92cdb1b39a28f7de420be9310575 Mon Sep 17 00:00:00 2001 From: Jay <359390851@qq.com> Date: Thu, 13 Apr 2017 19:22:27 +0800 Subject: [PATCH] fixed async --- README.md | 7 +++++- dist/lightings.js | 55 ++++++++++++++++++++++++++----------------- dist/lightings.min.js | 2 +- doc/README-zh.md | 5 ++++ package.json | 2 +- src/lightings.js | 55 ++++++++++++++++++++++++++----------------- test/a.html | 11 +++++---- 7 files changed, 87 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 1796a9a..bc1c3b4 100644 --- a/README.md +++ b/README.md @@ -120,4 +120,9 @@ modify lightings API, abolish some outdated configuration ### 2017 4.7 v2.0.1 -optimized template rendering \ No newline at end of file +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 \ No newline at end of file diff --git a/dist/lightings.js b/dist/lightings.js index e30dcc0..f3e749c 100644 --- a/dist/lightings.js +++ b/dist/lightings.js @@ -1,5 +1,5 @@ /*! - * Lightings v2.0.1 + * Lightings v2.0.2 * Copyright (c) 2017 Jay Zangwill */ 'use strict'; @@ -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; @@ -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); @@ -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(); @@ -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, diff --git a/dist/lightings.min.js b/dist/lightings.min.js index c61b204..a7341df 100644 --- a/dist/lightings.min.js +++ b/dist/lightings.min.js @@ -1 +1 @@ -"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};if(function(){function t(){this.xhr=new XMLHttpRequest,this.isIe9=navigator.userAgent.indexOf("MSIE 9.0")>0}function e(t){return"[object Object]"===t.toString()}function o(t){var e=[];for(var o in t)e.push(o+"="+t[o]);return t=e.join("&")}function i(t,i,s,r){if("string"!=typeof t)throw Error("url is no define or illegal");if(this.url=t,"string"==typeof i)this.data=i,this.options=s;else if(i&&"object"===(void 0===i?"undefined":_typeof(i))&&"undefined"==typeof optoins&&i.config){s={};for(var n in i)s[n]=i[n];i=void 0}else i&&e(i)&&(this.data=o(i));if(r)return void(this.callbackName=s&&s.callbackName||"callback");this.el=s&&s.el,this.flag=this.dataType=s&&s.dataType||"json",this.async=s&&s.async===!1||!0,this.contentType=s&&s.contentType||"application/x-www-form-urlencoded; charset=UTF-8",this.timeout=s&&s.timeout||0,this.progress=s&&s.progress,"xml"!==this.dataType&&"html"!==this.dataType||(this.dataType="")}function s(t,e){var o=void 0;return new Promise(function(i,s){if(e.xhr.responseType=e.dataType.toLowerCase(),"get"===t){var n=e.url;e.data&&(n+="?"+e.data),e.xhr.open(t,n,e.async),e.xhr.send(null)}else"post"===t&&(e.xhr.open(t,e.url,e.async),e.xhr.setRequestHeader("Content-type",e.contentType),e.xhr.send(e.data));e.xhr.onreadystatechange=function(){4===this.readyState&&(this.status>=200&&this.status<300||304===this.status?("xml"===e.flag?i(this.responseXML):e.isIe9?(i(JSON.parse(this.responseText)),r.call(e,JSON.parse(this.responseText))):(i(this.response),r.call(e,this.response)),o&&clearTimeout(o)):s(this.status,this.statusText))},e.async&&e.timeout>0&&(o=setTimeout(function(){e.xhr.abort(),s("timeout")},e.timeout)),e.xhr.onprogress=function(t){e.progress&&e.progress.call(this,t)}})}function r(t){var e=document.querySelector(this.el),o=void 0,i=t,s=void 0,r=void 0,n=void 0,a=!0,h=document.createDocumentFragment(),c=/{{(.+?)}}/g;if(this.el&&e&&"json"===this.dataType){if(void 0===e)throw Error("Cannot find element: "+this.el);if("body"===e.nodeName.toLowerCase()||"html"===e.nodeName.toLowerCase())throw Error("Do not mount Lightings to or - mount to normal elements instead.");for(;e.firstChild;)h.appendChild(e.firstChild);r=h.childNodes,Array.prototype.slice.call(r).forEach(function(e){for(a=!0,s="",n=e.textContent;c.test(n);)i=t,o=RegExp.$1.trim().split("."),o.forEach(function(t){i=i[t]}),a?(s=n.replace(/{{.+?}}/,i),a=!1):s=s.replace(/{{.+?}}/,i);e.textContent=s}),e.appendChild(h)}}t.prototype={constructor:t,get:function(t,e,o){return i.call(this,t,e,o),s("get",this)},post:function(t,e,o){return i.call(this,t,e,o),s("post",this)},jsonp:function(t,e,o){var s=this,r=document.createElement("script"),n=""+Math.random()+Math.random();return new Promise(function(a,h){try{i.call(s,t,e,o,!0),n=n.replace(/0\./g,"_"),r.src=s.data?s.url+"?"+s.callbackName+"=Lightings"+n+"&"+s.data:s.url+"?"+s.callbackName+"=Lightings"+n,document.body.appendChild(r),window["Lightings"+n]=a,document.body.removeChild(r)}catch(t){h(t)}})}},window.lightings=new t}(),!window.Promise){var _Promise=function t(e){var o=this;if(this.status="pending",this.thenCache=[],!(this instanceof t))throw'Defer is a constructor and should be called width "new" keyword';if("function"!=typeof e)throw"Defer params must be a function";setTimeout(function(){try{e.call(this,o.resolve.bind(o),o.reject.bind(o))}catch(t){o.reject(t)}},0)};_Promise.prototype.resolve=function(t){this.value=t,this.status="resolved",this.triggerThen()},_Promise.prototype.reject=function(t){this.value=t,this.status="rejected",this.triggerThen()},_Promise.prototype.then=function(t,e){return this.thenCache.push({onResolve:t,onReject:e}),this},_Promise.prototype.catch=function(t){"function"==typeof t&&(this.errorHandle=t)},_Promise.prototype.triggerThen=function(){var t,e=this.thenCache.shift();if(!e&&"resolved"===this.status)return this;if(!e&&"rejected"===this.status)return this.errorHandle&&(this.value=this.errorHandle.call(void 0,this.value),this.status="resolved"),this;if("resolved"===this.status?t=e.onResolve:"rejected"===this.status&&(t=e.onReject),"function"==typeof t)try{this.value=t.call(void 0,this.value),this.status="resolved",this.triggerThen()}catch(t){return this.status="rejected",this.value=t,this.triggerThen()}else this.triggerThen()},window.Promise=_Promise} \ No newline at end of file +"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};if(function(){function t(){this.xhr=new XMLHttpRequest,this.isIe9=navigator.userAgent.indexOf("MSIE 9.0")>0}function e(t){return"[object Object]"===t.toString()}function o(t){var e=[];for(var o in t)e.push(o+"="+t[o]);return t=e.join("&")}function s(t,s,i,n){if("string"!=typeof t)throw Error("url is no define or illegal");if(this.url=t,"string"==typeof s)this.data=s,this.options=i;else if(s&&"object"===(void 0===s?"undefined":_typeof(s))&&"undefined"==typeof optoins&&s.config){i={};for(var r in s)i[r]=s[r];s=void 0}else s&&e(s)&&(this.data=o(s));if(n)return void(this.callbackName=i&&i.callbackName||"callback");this.el=i&&i.el,this.flag=this.dataType=i&&i.dataType||"json",this.async=!i||i.async!==!1,this.contentType=i&&i.contentType||"application/x-www-form-urlencoded; charset=UTF-8",this.timeout=i&&i.timeout||0,this.progress=i&&i.progress,"xml"!==this.dataType&&"html"!==this.dataType||(this.dataType="")}function i(t,e){var o=void 0;return new Promise(function(s,i){if(e.async&&(e.xhr.responseType=e.dataType.toLowerCase()),e.xhr.onreadystatechange=function(){if(4===this.readyState)if(this.status>=200&&this.status<300||304===this.status){if("xml"===e.flag)s(this.responseXML);else if(e.isIe9||!e.async)try{s(JSON.parse(this.responseText)),n.call(e,JSON.parse(this.responseText))}catch(t){s(this.responseText)}else s(this.response),n.call(e,this.response);o&&clearTimeout(o)}else i(this.status,this.statusText)},"get"===t){var r=e.url;e.data&&(r+="?"+e.data),e.xhr.open(t,r,e.async),e.xhr.send(null)}else"post"===t&&(e.xhr.open(t,e.url,e.async),e.xhr.setRequestHeader("Content-type",e.contentType),e.xhr.send(e.data));e.async&&e.timeout>0&&(o=setTimeout(function(){e.xhr.abort(),i("timeout")},e.timeout)),e.xhr.onprogress=function(t){e.progress&&e.progress.call(this,t)}})}function n(t){var e=document.querySelector(this.el),o=void 0,s=void 0,i=void 0,n=void 0,r=void 0,a=!0,h=document.createDocumentFragment(),c=/{{(.+?)}}/g;if(this.el&&e&&"json"===this.dataType){if(void 0===e)throw Error("Cannot find element: "+this.el);if("body"===e.nodeName.toLowerCase()||"html"===e.nodeName.toLowerCase())throw Error("Do not mount Lightings to or - mount to normal elements instead.");for(;e.firstChild;)h.appendChild(e.firstChild);n=h.childNodes,Array.prototype.slice.call(n).forEach(function(e){for(a=!0,i="",r=e.textContent;c.test(r);)s=t,o=RegExp.$1.trim().split("."),o.forEach(function(t){s=s[t]}),a?(i=r.replace(/{{.+?}}/,s),a=!1):i=i.replace(/{{.+?}}/,s);e.textContent=i}),e.appendChild(h)}}t.prototype={constructor:t,get:function(t,e,o){return s.call(this,t,e,o),i("get",this)},post:function(t,e,o){return s.call(this,t,e,o),i("post",this)},jsonp:function(t,e,o){var i=this,n=document.createElement("script"),r=""+Math.random()+Math.random();return new Promise(function(a,h){try{s.call(i,t,e,o,!0),r=r.replace(/0\./g,"_"),n.src=i.data?i.url+"?"+i.callbackName+"=Lightings"+r+"&"+i.data:i.url+"?"+i.callbackName+"=Lightings"+r,document.body.appendChild(n),window["Lightings"+r]=a,document.body.removeChild(n)}catch(t){h(t)}})}},window.lightings=new t}(),!window.Promise){var _Promise=function t(e){var o=this;if(this.status="pending",this.thenCache=[],!(this instanceof t))throw'Defer is a constructor and should be called width "new" keyword';if("function"!=typeof e)throw"Defer params must be a function";setTimeout(function(){try{e.call(this,o.resolve.bind(o),o.reject.bind(o))}catch(t){o.reject(t)}},0)};_Promise.prototype.resolve=function(t){this.value=t,this.status="resolved",this.triggerThen()},_Promise.prototype.reject=function(t){this.value=t,this.status="rejected",this.triggerThen()},_Promise.prototype.then=function(t,e){return this.thenCache.push({onResolve:t,onReject:e}),this},_Promise.prototype.catch=function(t){"function"==typeof t&&(this.errorHandle=t)},_Promise.prototype.triggerThen=function(){var t,e=this.thenCache.shift();if(!e&&"resolved"===this.status)return this;if(!e&&"rejected"===this.status)return this.errorHandle&&(this.value=this.errorHandle.call(void 0,this.value),this.status="resolved"),this;if("resolved"===this.status?t=e.onResolve:"rejected"===this.status&&(t=e.onReject),"function"==typeof t)try{this.value=t.call(void 0,this.value),this.status="resolved",this.triggerThen()}catch(t){return this.status="rejected",this.value=t,this.triggerThen()}else this.triggerThen()},window.Promise=_Promise} \ No newline at end of file diff --git a/doc/README-zh.md b/doc/README-zh.md index 5532e9f..e7acf0f 100644 --- a/doc/README-zh.md +++ b/doc/README-zh.md @@ -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时无法获取数据的问题 \ No newline at end of file diff --git a/package.json b/package.json index 3921535..7daa1d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lightings", - "version": "2.0.1", + "version": "2.0.2", "description": "A lightweight Ajax Library", "keywords": [ "lightweight", diff --git a/src/lightings.js b/src/lightings.js index 9150c92..d71228a 100644 --- a/src/lightings.js +++ b/src/lightings.js @@ -1,5 +1,5 @@ /*! - * Lightings v2.0.1 + * Lightings v2.0.2 * Copyright (c) 2017 Jay Zangwill */ 'use strict'; @@ -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; @@ -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); @@ -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(); @@ -141,7 +154,7 @@ function compile(data) { let dom = document.querySelector(this.el), key, - variable = data, + variable, result, childs, text, diff --git a/test/a.html b/test/a.html index 148acd4..842571f 100644 --- a/test/a.html +++ b/test/a.html @@ -7,19 +7,20 @@ -
+