small refactoring
This commit is contained in:
parent
4d79d91c57
commit
a87e2a1414
2 changed files with 25 additions and 51 deletions
|
|
@ -887,10 +887,9 @@ Mithril = m = new function app(window, undefined) {
|
|||
m.request = function(xhrOptions) {
|
||||
if (xhrOptions.background !== true) m.startComputation()
|
||||
var deferred = m.deferred()
|
||||
var serialize = xhrOptions.serialize = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp"
|
||||
? identity : xhrOptions.serialize || JSON.stringify
|
||||
var deserialize = xhrOptions.deserialize = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp"
|
||||
? identity : xhrOptions.deserialize || JSON.parse
|
||||
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp"
|
||||
var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify
|
||||
var deserialize = xhrOptions.deserialize = isJSONP ? identity : xhrOptions.deserialize || JSON.parse
|
||||
var extract = xhrOptions.extract || function(xhr) {
|
||||
return xhr.responseText.length === 0 && deserialize === JSON.parse ? null : xhr.responseText
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1563,62 +1563,37 @@ function testMithril(mock) {
|
|||
mock.document.body = body;
|
||||
mock.document.appendChild(body);
|
||||
|
||||
var _window = mock;
|
||||
var error = m.prop("no error");
|
||||
var req = m.request({
|
||||
url: "/test",
|
||||
dataType: "jsonp",
|
||||
background: true
|
||||
}).then(null, error);
|
||||
var callbackKeys = [];
|
||||
Object.keys(_window).forEach(function(globalKey){
|
||||
if(globalKey.indexOf("mithril_callback") > -1)
|
||||
callbackKeys.push(globalKey);
|
||||
});
|
||||
var foundScriptTag = false, scriptTag = null;
|
||||
mock.document.getElementsByTagName("script").forEach(function(script){
|
||||
if(!scriptTag && script.src.indexOf(callbackKeys[0]) > -1)
|
||||
scriptTag = script;
|
||||
});
|
||||
if(scriptTag){
|
||||
foundScriptTag = true;
|
||||
delete _window[callbackKeys[0]];
|
||||
mock.document.body.removeChild(scriptTag);
|
||||
}
|
||||
var data
|
||||
var req = m.request({url: "/test", dataType: "jsonp"}).then(function(received) {data = received}, error);
|
||||
var callbackKey = Object.keys(mock).filter(function(globalKey){
|
||||
return globalKey.indexOf("mithril_callback") > -1
|
||||
}).pop();
|
||||
var scriptTag = [].slice.call(mock.document.getElementsByTagName("script")).filter(function(script){
|
||||
return script.src.indexOf(callbackKey) > -1
|
||||
}).pop();
|
||||
mock[callbackKey]({foo: "bar"})
|
||||
mock.document.removeChild(body);
|
||||
return callbackKeys.length === 1 && foundScriptTag;
|
||||
return scriptTag.src.indexOf("/test?callback=mithril_callback") > -1 && data.foo == "bar";
|
||||
})
|
||||
test(function(){
|
||||
// script tags cannot be appended directly on the document
|
||||
var body = mock.document.createElement("body");
|
||||
mock.document.body = body;
|
||||
mock.document.appendChild(body);
|
||||
|
||||
var _window = mock;
|
||||
var error = m.prop(false);
|
||||
var req = m.request({
|
||||
url: "/test",
|
||||
dataType: "jsonp",
|
||||
background: true,
|
||||
callbackKey: "jsonpCallback"
|
||||
}).then(null, error);
|
||||
var callbackKeys = [];
|
||||
Object.keys(_window).forEach(function(globalKey){
|
||||
if(globalKey.indexOf("mithril_callback") > -1)
|
||||
callbackKeys.push(globalKey);
|
||||
});
|
||||
var scriptTag = null;
|
||||
mock.document.getElementsByTagName("script").forEach(function(script){
|
||||
if(!scriptTag && script.src.indexOf(callbackKeys[0]) > -1)
|
||||
scriptTag = script;
|
||||
});
|
||||
var correctCallback = false;
|
||||
if(scriptTag){
|
||||
correctCallback = scriptTag.src.indexOf("jsonpCallback") > -1;
|
||||
delete _window[callbackKeys[0]];
|
||||
mock.document.body.removeChild(scriptTag);
|
||||
}
|
||||
var error = m.prop("no error");
|
||||
var data
|
||||
var req = m.request({url: "/test", dataType: "jsonp", callbackKey: "jsonpCallback"}).then(function(received) {data = received}, error);
|
||||
var callbackKey = Object.keys(mock).filter(function(globalKey){
|
||||
return globalKey.indexOf("mithril_callback") > -1
|
||||
}).pop();
|
||||
var scriptTag = [].slice.call(mock.document.getElementsByTagName("script")).filter(function(script){
|
||||
return script.src.indexOf(callbackKey) > -1
|
||||
}).pop();
|
||||
mock[callbackKey]({foo: "bar1"})
|
||||
mock.document.removeChild(body);
|
||||
return correctCallback;
|
||||
return scriptTag.src.indexOf("/test?jsonpCallback=mithril_callback") > -1 && data.foo == "bar1";
|
||||
})
|
||||
test(function(){
|
||||
var body = mock.document.createElement("body");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue