我正在编写一个使用require.js作为我的加载框架的移动混合应用程序。我有加载错误的问题。我正在尝试做的是在设备离线时设置后备解决方案,我无法下载需要在屏幕上显示地图的google maps API脚本。我得到的只是
I'm writing a mobile hybrid app using require.js as my loading framework. I have an issue with loading errors. What I'm trying to do is setup a fallback solution when the device is offline and I can't download the google maps API script that I need to display a map on the screen. All that I get is
Uncaught Error: Load timeout for modules: async!maps.googleapis/maps/api/js?sensor=true但我无法发现此错误并提供替代实施。这是我的gmaps模块定义
but I'm not able to catch this error and provide an alternative implementation. Here is my gmaps module definition
define('gmaps', ['async!maps.googleapis/maps/api/js?sensor=true'],function(){ return window.google.maps; });我该怎么办?
编辑
由于您的帮助,我设法找到了可能的解决方案。我已经设置了这样的要求
I managed to find a possible solution thanks to your help. I've setup require like this
require.config({ paths: { gmaps: ['maps.googleapis/maps/api/js?sensor=true', 'lib/dummymaps'] } }dummymaps只是一个简单的模块:
dummymaps is only 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 that's a good solution?
编辑2:
原谅我,它没有合作这个代码。它总是回到替代实现,因为gmaps需要使用异步插件来完全加载ed,我无法使其与插件一起使用。
Forgive me, it's not working with this code. It's always falling back to the alternative implementation because gmaps needs to use async plugin to be fully loaded and I'm not able to make it work with the plugin.
推荐答案您可以通过以下方式捕获错误:
You can catch the error by :
requirejs.onError = function (err) { if (err.requireType === 'timeout') { alert("error: "+err); } else { throw err; } };希望这有帮助!
更多推荐
如何处理requireJs超时错误?
发布评论