Skip to content

Commit 0cd83ea

Browse files
illyaVvengrov
authored andcommitted
Possibility to patch ajax instanse (#61)
* feat (SDK ajax): possibility to patch ajax instanse * feat (SDK ajax): possibility to patch xmlHttp instanse * feat (SDK ajax): possibility to patch xmlHttp instanse
1 parent e9e9511 commit 0cd83ea

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

libs/backendless.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
return this;
4747
};
4848

49+
Backendless.XMLHttpRequest = XMLHttpRequest;
50+
4951
if (!Array.prototype.indexOf) {
5052
Array.prototype.indexOf = function(searchElement, fromIndex) {
5153
var k;
@@ -75,8 +77,6 @@
7577
};
7678
}
7779

78-
initXHR();
79-
8080
var browser = (function() {
8181
var ua = 'NodeJS';
8282

@@ -177,22 +177,15 @@
177177
}
178178
};
179179

180-
function initXHR() {
181-
try {
182-
if (typeof XMLHttpRequest.prototype.sendAsBinary == 'undefined') {
183-
XMLHttpRequest.prototype.sendAsBinary = function(text) {
184-
var data = new ArrayBuffer(text.length);
185-
var ui8a = new Uint8Array(data, 0);
186-
for (var i = 0; i < text.length; i++) {
187-
ui8a[i] = (text.charCodeAt(i) & 0xff);
188-
}
189-
this.send(ui8a);
190-
};
191-
}
192-
}
193-
catch (e) {
180+
Utils.stringToBiteArray = function(str) {
181+
var data = new ArrayBuffer(str.length);
182+
var ui8a = new Uint8Array(data, 0);
183+
for (var i = 0; i < str.length; i++) {
184+
ui8a[i] = (str.charCodeAt(i) & 0xff);
194185
}
195-
}
186+
187+
return ui8a;
188+
};
196189

197190
function tryParseJSON(s) {
198191
try {
@@ -275,7 +268,7 @@
275268
}
276269
},
277270
sendRequest = function(config) {
278-
var xhr = new XMLHttpRequest(),
271+
var xhr = new Backendless.XMLHttpRequest(),
279272
contentType = config.data ? 'application/json' : 'application/x-www-form-urlencoded',
280273
response;
281274

@@ -452,7 +445,9 @@
452445
return req.end();
453446
};
454447

455-
Backendless._ajax = isBrowser ? Backendless._ajax_for_browser : Backendless._ajax_for_nodejs;
448+
Backendless._ajax = function(config){
449+
return Backendless.XMLHttpRequest ? Backendless._ajax_for_browser(config) : Backendless._ajax_for_nodejs(config);
450+
};
456451

457452
var getClassName = function() {
458453
if (this.prototype && this.prototype.___class) {
@@ -3375,7 +3370,7 @@
33753370
var async = options.async;
33763371
var encoded = options.encoded;
33773372
var boundary = '-backendless-multipart-form-boundary-' + getNow();
3378-
var xhr = new XMLHttpRequest();
3373+
var xhr = new Backendless.XMLHttpRequest();
33793374

33803375
var badResponse = function (xhr) {
33813376
var result = {};
@@ -3425,11 +3420,7 @@
34253420
};
34263421
}
34273422

3428-
if (encoded) {
3429-
xhr.send(options.data);
3430-
} else {
3431-
xhr.sendAsBinary(getBuilder(options.fileName, options.data, boundary));
3432-
}
3423+
xhr.send(encoded ? options.data : Utils.stringToBiteArray(getBuilder(options.fileName, options.data, boundary)));
34333424

34343425
if (async) {
34353426
return xhr;

0 commit comments

Comments
 (0)