-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08b6f2c
commit 37e4de7
Showing
4 changed files
with
118 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,40 @@ | ||
/* eslint-env browser */ | ||
(function($, http) { | ||
"use strict"; | ||
|
||
var nonce = Date.now(); | ||
|
||
var httpClient = new http.HttpClient( | ||
new http.HttpXhrBackend({ | ||
build: function() { | ||
return new XMLHttpRequest(); | ||
} | ||
}) | ||
); | ||
|
||
function createXhrSurrogate(response) { | ||
function getResponseHeader(name) { | ||
return response.headers.get(name); | ||
} | ||
|
||
function makeResponseText() { | ||
var error = response.error; | ||
if(error === undefined) | ||
return "N/A"; | ||
|
||
if(typeof error !== "string" || String(getResponseHeader("Content-Type")).indexOf("application/json") === 0) | ||
return JSON.stringify(error); | ||
|
||
return error; | ||
} | ||
|
||
return { | ||
status: response.status, | ||
statusText: response.statusText, | ||
getResponseHeader: getResponseHeader, | ||
responseText: makeResponseText() | ||
}; | ||
} | ||
|
||
$.ajax = function(options) { | ||
var d = $.Deferred(); | ||
|
||
var method = (options.method || "get").toLowerCase(); | ||
var data = options.data; | ||
var xhrFields = options.xhrFields; | ||
|
||
if(options.cache === false && method === "get" && data) | ||
data._ = nonce++; | ||
(function() { | ||
"use strict"; | ||
|
||
httpClient | ||
.request( | ||
method, | ||
options.url, | ||
{ | ||
params: data, | ||
responseType: options.dataType, | ||
headers: options.headers, | ||
withCredentials: xhrFields && xhrFields.withCredentials, | ||
observe: "response" | ||
function createHttpClient(http) { | ||
return new http.HttpClient( | ||
new http.HttpXhrBackend({ | ||
build: function() { | ||
return new XMLHttpRequest(); | ||
} | ||
) | ||
.subscribe( | ||
function(response) { | ||
d.resolve(response.body, "success", createXhrSurrogate(response)); | ||
}, | ||
function(response) { | ||
d.reject(createXhrSurrogate(response), "error"); | ||
} | ||
); | ||
|
||
return d.promise(); | ||
}; | ||
}) | ||
); | ||
} | ||
|
||
})(window.jQuery, window.ng.common.http); | ||
var ajax; | ||
var createNgHttpClientSendRequestFunc; | ||
|
||
if(typeof define === "function" && define.amd) { | ||
define(function(require) { | ||
ajax = require("devextreme/core/utils/ajax"); | ||
createNgHttpClientSendRequestFunc = require("./ng-http-client-helper"); | ||
|
||
ajax.inject({ | ||
sendRequest: createNgHttpClientSendRequestFunc( | ||
createHttpClient(require("@angular/common/bundles/common-http.umd")), | ||
require("devextreme/core/utils/deferred").Deferred | ||
) | ||
}); | ||
}); | ||
} else if (typeof module === "object" && module.exports) { | ||
throw "TODO"; | ||
} else { | ||
var $ = window.jQuery; | ||
$.ajax = window.DevExpress.data.AspNet.createNgHttpClientSendRequestFunc( | ||
createHttpClient(window.ng.common.http), | ||
$.Deferred | ||
); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-env browser */ | ||
|
||
(function(sendRequestFactory) { | ||
"use strict"; | ||
|
||
if(typeof define === "function" && define.amd) { | ||
define(function(require, exports, module) { | ||
module.exports = sendRequestFactory; | ||
}); | ||
} else if (typeof module === "object" && module.exports) { | ||
throw "TODO"; | ||
} else { | ||
window.DevExpress.data.AspNet.createNgHttpClientSendRequestFunc = sendRequestFactory; | ||
} | ||
|
||
})(function(httpClient, Deferred) { | ||
"use strict"; | ||
|
||
var nonce = Date.now(); | ||
|
||
function createXhrSurrogate(response) { | ||
function getResponseHeader(name) { | ||
return response.headers.get(name); | ||
} | ||
|
||
function makeResponseText() { | ||
var error = response.error; | ||
if(error === undefined) | ||
return "N/A"; | ||
|
||
if(typeof error !== "string" || String(getResponseHeader("Content-Type")).indexOf("application/json") === 0) | ||
return JSON.stringify(error); | ||
|
||
return error; | ||
} | ||
|
||
return { | ||
status: response.status, | ||
statusText: response.statusText, | ||
getResponseHeader: getResponseHeader, | ||
responseText: makeResponseText() | ||
}; | ||
} | ||
|
||
return function(options) { | ||
var d = Deferred(); | ||
|
||
var method = (options.method || "get").toLowerCase(); | ||
var data = options.data; | ||
var xhrFields = options.xhrFields; | ||
|
||
if(options.cache === false && method === "get" && data) | ||
data._ = nonce++; | ||
|
||
httpClient | ||
.request( | ||
method, | ||
options.url, | ||
{ | ||
params: data, | ||
responseType: options.dataType, | ||
headers: options.headers, | ||
withCredentials: xhrFields && xhrFields.withCredentials, | ||
observe: "response" | ||
} | ||
) | ||
.subscribe( | ||
function(response) { | ||
d.resolve(response.body, "success", createXhrSurrogate(response)); | ||
}, | ||
function(response) { | ||
d.reject(createXhrSurrogate(response), "error"); | ||
} | ||
); | ||
|
||
return d.promise(); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters