small refactoring

This commit is contained in:
Leo Horie 2014-09-01 10:42:15 -04:00
parent 4d79d91c57
commit a87e2a1414
2 changed files with 25 additions and 51 deletions

View file

@ -887,10 +887,9 @@ Mithril = m = new function app(window, undefined) {
m.request = function(xhrOptions) { m.request = function(xhrOptions) {
if (xhrOptions.background !== true) m.startComputation() if (xhrOptions.background !== true) m.startComputation()
var deferred = m.deferred() var deferred = m.deferred()
var serialize = xhrOptions.serialize = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp" var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp"
? identity : xhrOptions.serialize || JSON.stringify var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify
var deserialize = xhrOptions.deserialize = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp" var deserialize = xhrOptions.deserialize = isJSONP ? identity : xhrOptions.deserialize || JSON.parse
? identity : xhrOptions.deserialize || JSON.parse
var extract = xhrOptions.extract || function(xhr) { var extract = xhrOptions.extract || function(xhr) {
return xhr.responseText.length === 0 && deserialize === JSON.parse ? null : xhr.responseText return xhr.responseText.length === 0 && deserialize === JSON.parse ? null : xhr.responseText
} }

View file

@ -1563,62 +1563,37 @@ function testMithril(mock) {
mock.document.body = body; mock.document.body = body;
mock.document.appendChild(body); mock.document.appendChild(body);
var _window = mock;
var error = m.prop("no error"); var error = m.prop("no error");
var req = m.request({ var data
url: "/test", var req = m.request({url: "/test", dataType: "jsonp"}).then(function(received) {data = received}, error);
dataType: "jsonp", var callbackKey = Object.keys(mock).filter(function(globalKey){
background: true return globalKey.indexOf("mithril_callback") > -1
}).then(null, error); }).pop();
var callbackKeys = []; var scriptTag = [].slice.call(mock.document.getElementsByTagName("script")).filter(function(script){
Object.keys(_window).forEach(function(globalKey){ return script.src.indexOf(callbackKey) > -1
if(globalKey.indexOf("mithril_callback") > -1) }).pop();
callbackKeys.push(globalKey); mock[callbackKey]({foo: "bar"})
});
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);
}
mock.document.removeChild(body); mock.document.removeChild(body);
return callbackKeys.length === 1 && foundScriptTag; return scriptTag.src.indexOf("/test?callback=mithril_callback") > -1 && data.foo == "bar";
}) })
test(function(){ test(function(){
// script tags cannot be appended directly on the document
var body = mock.document.createElement("body"); var body = mock.document.createElement("body");
mock.document.body = body; mock.document.body = body;
mock.document.appendChild(body); mock.document.appendChild(body);
var _window = mock; var error = m.prop("no error");
var error = m.prop(false); var data
var req = m.request({ var req = m.request({url: "/test", dataType: "jsonp", callbackKey: "jsonpCallback"}).then(function(received) {data = received}, error);
url: "/test", var callbackKey = Object.keys(mock).filter(function(globalKey){
dataType: "jsonp", return globalKey.indexOf("mithril_callback") > -1
background: true, }).pop();
callbackKey: "jsonpCallback" var scriptTag = [].slice.call(mock.document.getElementsByTagName("script")).filter(function(script){
}).then(null, error); return script.src.indexOf(callbackKey) > -1
var callbackKeys = []; }).pop();
Object.keys(_window).forEach(function(globalKey){ mock[callbackKey]({foo: "bar1"})
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);
}
mock.document.removeChild(body); mock.document.removeChild(body);
return correctCallback; return scriptTag.src.indexOf("/test?jsonpCallback=mithril_callback") > -1 && data.foo == "bar1";
}) })
test(function(){ test(function(){
var body = mock.document.createElement("body"); var body = mock.document.createElement("body");