私はhvgotcodesの提案に基づいてソリューションをすばやくコーディングしました。SC.XHRResponse
以下のコードは、IE固有のXDomainRequestCORSリクエストを処理するために必要な機能を追加するサブクラスを実装しています。XDomainRequestを使用している場合、これはIEのエラーをまだ処理しないことに注意してください。
MyApp.CorsRequest = SC.XHRResponse.extend({
createRequest: function() {
function tryThese() {
for (var i=0; i < arguments.length; i++) {
try {
var item = arguments[i]();
return item;
} catch (e) {}
}
return NO;
}
return tryThese(
function() { return new XDomainRequest(); },
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject('Msxml2.XMLHTTP'); },
function() { return new ActiveXObject('Microsoft.XMLHTTP'); }
);
},
invokeTransport: function() {
var rawRequest, transport, handleReadyStateChange, async, headers;
rawRequest = this.createRequest();
this.set('rawRequest', rawRequest);
// configure async callback - differs per browser...
async = !!this.getPath('request.isAsynchronous');
if (async) {
if (!SC.browser.isIE && !SC.browser.isOpera) {
SC.Event.add(rawRequest, 'readystatechange', this,
this.finishRequest, rawRequest);
} else if(SC.browser.isIE) {
transport = this;
handleLoad = function() {
if (!transport) { return null; }
var ret = transport.finishRequest();
if (ret) { transport = null; }
return ret;
};
rawRequest.onload = handleLoad;
} else {
transport = this;
handleReadyStateChange = function() {
if (!transport) { return null; }
var ret = transport.finishRequest();
if (ret) { transport = null; }
return ret;
};
rawRequest.onreadystatechange = handleReadyStateChange;
}
}
// initiate request.
rawRequest.open(this.get('type'), this.get('address'), async);
// now send the actual request body - for sync requests browser will
// block here
rawRequest.send(this.getPath('request.encodedBody')) ;
if (!async) { this.finishRequest(); }
return rawRequest;
},
finishRequest: function(evt) {
var rawRequest = this.get('rawRequest'),
readyState = rawRequest.readyState,
error, status, msg;
if (SC.browser.isIE) {
readyState = 4;
rawRequest.status = 200;
}
if (readyState === 4 && !this.get('timedOut')) {
this.receive(function(proceed) {
if (!proceed) { return; }
// collect the status and decide if we're in an error state or not
status = -1;
try {
status = rawRequest.status || 0;
} catch (e) {}
// if there was an error - setup error and save it
if ((status < 200) || (status >= 300)) {
try {
msg = rawRequest.statusText || '';
} catch(e2) {
msg = '';
}
error = SC.$error(msg || "HTTP Request failed", "Request", status);
error.set("errorValue", this) ;
this.set('isError', YES);
this.set('errorObject', error);
}
// set the status - this will trigger changes on related properties
this.set('status', status);
}, this);
// Avoid memory leaks
if (!SC.browser.isIE && !SC.browser.isOpera) {
SC.Event.remove(rawRequest, 'readystatechange', this, this.finishRequest);
} else {
if (window.XDomainRequest)
rawRequest.onload = null;
else
rawRequest.onreadystatechange = null;
}
return YES;
}
return NO;
}
});