This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0

因为不支持自定义协议的缘故,放弃fetch的使用,改回xhr

master 2.2.1
宇天 2019-09-09 20:18:17 +08:00
parent fe1fd505ed
commit f795667b5b
2 changed files with 27 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{
"name": "anot",
"version": "2.2.0",
"version": "2.2.1",
"description": "Anot - 迷你mvvm框架",
"main": "dist/anot.js",
"files": ["dist"],

View File

@ -20,6 +20,26 @@ function nodesToFrag(nodes) {
}
return frag
}
function _fetch(url) {
var xhr = new XMLHttpRequest()
var defer = Promise.defer()
xhr.open('GET', url, true)
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
xhr.responseType = 'text'
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
defer.resolve(this.response)
} else {
defer.reject(this)
}
}
}
xhr.send(null)
return defer.promise
}
Anot.directive('include', {
init: directives.attr.init,
update: function(val) {
@ -141,27 +161,17 @@ Anot.directive('include', {
scanTemplate(templatePool[val])
})
} else {
fetch(val, {
method: 'get',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(res => {
if (res.status >= 200 && res.status < 300) {
return res.text()
} else {
return Promise.reject(
`获取网络资源出错, ${res.status} (${res.statusText})`
)
}
})
_fetch(val)
.then(text => {
templatePool[val] = text
scanTemplate(text)
})
.catch(err => {
log(':include load [' + val + '] error\n%c%s', 'color:#f30', err)
log(
':include load [' + val + '] error\n%c%s',
'color:#f30',
`获取网络资源出错, ${err.status} (${err.statusText})`
)
})
}
}