more docs
This commit is contained in:
parent
d6e18ca134
commit
3a19dddb22
5 changed files with 113 additions and 24 deletions
|
|
@ -40,16 +40,7 @@ module.exports = function($window) {
|
|||
try {
|
||||
var response = args.deserialize(args.extract(xhr, args))
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
if (typeof args.type === "function") {
|
||||
if (response instanceof Array) {
|
||||
for (var i = 0; i < response.length; i++) {
|
||||
response[i] = new args.type(response[i])
|
||||
}
|
||||
}
|
||||
else response = new args.type(response)
|
||||
}
|
||||
|
||||
stream(response)
|
||||
stream(cast(args.type, response))
|
||||
}
|
||||
else {
|
||||
var error = new Error(xhr.responseText)
|
||||
|
|
@ -72,12 +63,13 @@ module.exports = function($window) {
|
|||
|
||||
function jsonp(args) {
|
||||
var stream = Stream.stream()
|
||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||
|
||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||
var script = $window.document.createElement("script")
|
||||
$window[callbackName] = function(data) {
|
||||
script.parentNode.removeChild(script)
|
||||
stream(data)
|
||||
stream(cast(args.type, data))
|
||||
if (typeof oncompletion === "function") oncompletion()
|
||||
delete $window[callbackName]
|
||||
}
|
||||
|
|
@ -125,5 +117,17 @@ module.exports = function($window) {
|
|||
|
||||
function extract(xhr) {return xhr.responseText}
|
||||
|
||||
function cast(type, data) {
|
||||
if (typeof type === "function") {
|
||||
if (data instanceof Array) {
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
data[i] = new type(data[i])
|
||||
}
|
||||
}
|
||||
else return new type(data)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
return {xhr: xhr, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue