How to handle timejs require error?

I am writing a mobile hybrid application using require.js as my download platform. I have a problem with loading errors. What I'm trying to do is install a backup solution when the device is disconnected and I cannot load the google maps script API that I need to display the map on the screen. All I get is

Uncaught Error: Load timeout for modules: async!http://maps.googleapis.com/maps/api/js?sensor=true 

but I cannot catch this error and provide an alternative implementation. Here is my gmaps module definition

 define('gmaps', ['async!http://maps.googleapis.com/maps/api/js?sensor=true'],function(){ return window.google.maps; }); 

What can I do?

EDIT

I managed to find a possible solution thanks to your help. I need to configure such settings

 require.config({ paths: { gmaps: ['http://maps.googleapis.com/maps/api/js?sensor=true', 'lib/dummymaps'] } } 

dummymaps is just a simple module:

 define({ dummy: true }); 

Then in my โ€œparentโ€ module I do:

 define(["gmaps"],function(gmaps){ ... if(typeof gmaps.dummy != 'undefined' && gmaps.dummy == true){ // use a local image as map } else { // initialize google maps canvas } }); 

Do you think this is a good solution?

EDIT 2:

Forgive me, it does not work with this code. It always returns to an alternative implementation, because gmaps needs to use the async plugin to fully load, and I cannot get it to work with the plugin.

+6
source share
5 answers

You can catch the error:

 requirejs.onError = function (err) { if (err.requireType === 'timeout') { alert("error: "+err); } else { throw err; } }; 

Hope this helps!

+6
source

You can try the following:

 requirejs.config({ paths: { // define an alias here, first try remote, if it fails it will try your local file 'gmaps': ['http://maps.googleapis.com/maps/api/js?sensor=true', 'your local file.js'] } }); define('gmaps', function(gm) { // var gm should now be the google maps plugin console.log(gm); }); 
+2
source

I had the same problem and solved it with this snippet:

 require(['async!http://maps.google.com/maps/api/js?sensor=false'], function() { //Success processing }, function (e) { //Error processing }); 
+2
source

I believe that your original problem was never solved exactly the way you wanted, and we faced the same issue. Basically, we need a way to restore grace when Google maps were disabled or were unavailable, since there are times when our application runs on a user computer where they do not have Internet access.

I originally used async! a plugin that everyone else uses, but could not come up with a modification that would allow me to handle errors.

Therefore, I wrote my own utility class that loads Google maps asynchronously outside the requireJS container and provides error callback handling. Take a look and see if it solves your problem. For us, this allows us to display a good warning view to the user when it is not possible to reach maps.google.com, and the rest of the application is still loading normally:

https://github.com/mag382/googleMapsLoaderUtil

+1
source

The RequireJS documentation contains a bug section that will cover your specific case.

Details revert to a local copy of jQuery if the CDN is not available.

0
source

Source: https://habr.com/ru/post/952098/


All Articles