knockout-amd-helpers
Version:
Makes Knockout and AMD modules work better together
79 lines (65 loc) • 1.82 kB
JavaScript
/** MIT License (c) copyright B Cavalier & J Hann */
/**
* curl text! loader plugin
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*/
/**
* TODO: load xdomain text, too
*
*/
define(/*=='curl/plugin/text',==*/ function () {
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
function xhr () {
if (typeof XMLHttpRequest !== "undefined") {
// rewrite the getXhr method to always return the native implementation
xhr = function () { return new XMLHttpRequest(); };
}
else {
// keep trying progIds until we find the correct one, then rewrite the getXhr method
// to always return that one.
var noXhr = xhr = function () {
throw new Error("getXhr(): XMLHttpRequest not available");
};
while (progIds.length > 0 && xhr === noXhr) (function (id) {
try {
new ActiveXObject(id);
xhr = function () { return new ActiveXObject(id); };
}
catch (ex) {}
}(progIds.shift()));
}
return xhr();
}
function fetchText (url, callback, errback) {
var x = xhr();
x.open('GET', url, true);
x.onreadystatechange = function (e) {
if (x.readyState === 4) {
if (x.status < 400) {
callback(x.responseText);
}
else {
errback(new Error('fetchText() failed. status: ' + x.statusText));
}
}
};
x.send(null);
}
function error (ex) {
throw ex;
}
return {
// 'normalize': function (resourceId, toAbsId) {
// // remove options
// return resourceId ? toAbsId(resourceId.split("!")[0]) : resourceId;
// },
load: function (resourceName, req, callback, config) {
// remove suffixes (future)
// get the text
fetchText(req['toUrl'](resourceName), callback, callback['error'] || error);
},
'plugin-builder': './builder/text'
};
});