javascript - Requirejs ignore loading error for optional dependency -
this question has answer here:
- requirejs optional dependency 3 answers
i'm using require.js , load library handles tracking. however, have users block loading.
since not critical part of app, still work, when tracking library fails load.
i've looked @ documentation handling errors via errbacks, config fallbacks, , global onerror function.
i thinking of like:
requirejs.onerror = function (err) { var modules = err.requiremodules; (var = 0; < modules.length; i++) { if (modules[i] == 'tracking-lib') { // great if define('modules[i]', [], null) } } };
similar questions (that don't solve problem):
i have created little require plugin (code in github) can lazy-load amd modules, e.g. this:
define(["lazy!mymodule"], function(mymodule) { mymodule.get().then( // get() returns promise function(m) { // handle success, module in m argument }, function(e) { // handle error } ); });
you use is. alternatively, create similar plugin, e.g. optional
, without depending on require-lazy. optional
plugin used as:
define(["optional!mymodule"], function(mymodule) { // code above // or there may way make optional! return null, if loading failed
the code my plugin might of , of course docs plugin api.
still simpler - imho dirtier - way require
optional module inside client module, using global require
function:
define([], function() { // use global require!!! require(["myoptionalmodule"], function(myoptionalmodule) { // loaded }, function(error) { // load failed } ); });
(also take here - related last code)
in conclusion, don't think there way load module optionally plain api. have implement somehow , handle asynchronicity callbacks, above, either inside application code or in plugin.
Comments
Post a Comment