Skip to content

Commit df43b24

Browse files
move jquery to external var
1 parent 3637eb4 commit df43b24

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

src/oc-client.js

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ var oc = oc || {};
6868
};
6969

7070
// constants
71-
var JQUERY_URL =
72-
'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
73-
RETRY_INTERVAL = ocConf.retryInterval || __DEFAULT_RETRY_INTERVAL__,
71+
var RETRY_INTERVAL = ocConf.retryInterval || __DEFAULT_RETRY_INTERVAL__,
7472
RETRY_LIMIT = ocConf.retryLimit || __DEFAULT_RETRY_LIMIT__,
7573
DISABLE_LOADER = isBool(ocConf.disableLoader)
7674
? ocConf.disableLoader
@@ -199,17 +197,17 @@ var oc = oc || {};
199197
};
200198

201199
var asyncRequireForEach = function (toLoad, loaded, callback) {
202-
if (isFunction(loaded)) {
200+
if (!callback) {
203201
callback = loaded;
204202
loaded = [];
205203
}
206204

207205
if (!toLoad.length) {
208-
callback();
206+
callback(loaded);
209207
} else {
210208
var loading = toLoad[0];
211-
oc.require(loading.global, loading.url, function () {
212-
asyncRequireForEach(toLoad.slice(1), loaded.concat(loading), callback);
209+
oc.require(loading.global, loading.url, function (resolved) {
210+
asyncRequireForEach(toLoad.slice(1), loaded.concat(resolved), callback);
213211
});
214212
}
215213
};
@@ -265,9 +263,6 @@ var oc = oc || {};
265263
]
266264
};
267265
var headers = getHeaders();
268-
if (jsonRequest) {
269-
headers['Content-Type'] = 'application/json';
270-
}
271266
var ajaxOptions = {
272267
method: 'POST',
273268
url: baseUrl,
@@ -283,6 +278,7 @@ var oc = oc || {};
283278
};
284279
if (jsonRequest) {
285280
ajaxOptions.dataType = 'json';
281+
headers['Content-Type'] = 'application/json';
286282
}
287283

288284
$.ajax(ajaxOptions);
@@ -323,27 +319,25 @@ var oc = oc || {};
323319
isRequired('name', options.name);
324320

325321
var withFinalSlash = function (s) {
322+
if (!s) return '';
323+
326324
return s.match(/\/$/) ? s : s + '/';
327325
};
328326

329-
var href = withFinalSlash(options.baseUrl) + withFinalSlash(options.name);
330-
331-
if (options.version) {
332-
href += withFinalSlash(options.version);
333-
}
327+
var href =
328+
withFinalSlash(options.baseUrl) +
329+
withFinalSlash(options.name) +
330+
withFinalSlash(options.version);
334331

335332
if (options.parameters) {
336333
href += '?';
337-
for (var parameter in options.parameters) {
338-
// eslint-disable-next-line no-prototype-builtins
339-
if (options.parameters.hasOwnProperty(parameter)) {
340-
var value = options.parameters[parameter];
341-
if (/[+&=]/.test(value)) {
342-
value = encodeURIComponent(value);
343-
}
344-
href += parameter + '=' + value + '&';
334+
$.each(options.parameters, function (key, value) {
335+
if (/[+&=]/.test(value)) {
336+
value = encodeURIComponent(value);
345337
}
346-
}
338+
href += key + '=' + value + '&';
339+
});
340+
347341
href = href.slice(0, -1);
348342
}
349343

@@ -391,15 +385,14 @@ var oc = oc || {};
391385
};
392386
};
393387

394-
oc.require('jQuery', JQUERY_URL, function (jQuery) {
395-
oc.requireSeries(externals, function () {
396-
if ($window.jQuery || $window.$) {
397-
$ = oc.$ = jQuery;
398-
} else {
399-
$ = oc.$ = jQuery.noConflict();
400-
}
401-
done();
402-
});
388+
oc.requireSeries(externals, function (deps) {
389+
var jQuery = deps[0];
390+
if ($window.jQuery || $window.$) {
391+
$ = oc.$ = jQuery;
392+
} else {
393+
$ = oc.$ = jQuery.noConflict();
394+
}
395+
done();
403396
});
404397
}
405398
};
@@ -568,28 +561,22 @@ var oc = oc || {};
568561
oc.renderUnloadedComponents = function () {
569562
oc.ready(function () {
570563
var $unloadedComponents = $(
571-
OC_TAG + '[data-rendered!=true][data-failed!=true]'
572-
),
573-
toDo = $unloadedComponents.length;
574-
575-
if (toDo > 0) {
576-
$.each($unloadedComponents, function (_idx, unloadedComponent) {
577-
oc.renderNestedComponent(unloadedComponent, function () {
578-
toDo--;
579-
if (!toDo) {
580-
oc.renderUnloadedComponents();
581-
}
582-
});
564+
OC_TAG + '[data-rendered!=true][data-failed!=true]'
565+
);
566+
567+
$unloadedComponents.map(function (idx, unloadedComponent) {
568+
oc.renderNestedComponent(unloadedComponent, function () {
569+
if (idx == $unloadedComponents.length - 1) {
570+
oc.renderUnloadedComponents();
571+
}
583572
});
584-
}
573+
});
585574
});
586575
};
587576

588577
oc.load = function (placeholder, href, callback) {
589578
oc.ready(function () {
590-
if (!isFunction(callback)) {
591-
callback = noop;
592-
}
579+
callback = callback || noop;
593580

594581
if (placeholder) {
595582
$(placeholder).html('<' + OC_TAG + ' href="' + href + '" />');

tasks/compile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ function transformTemplates(templates = {}) {
4949
}
5050

5151
function parseConf(conf) {
52+
const jQueryExternal = {
53+
global: 'jQuery',
54+
url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'
55+
};
56+
5257
return {
53-
externals: conf.externals || [],
58+
externals: [jQueryExternal].concat(conf.externals || []),
5459
retryLimit: conf.retryLimit || 30,
5560
retryInterval: conf.retryInterval || 5000,
5661
disableLoader: Boolean(conf.disableLoader ?? false),

0 commit comments

Comments
 (0)