Jus*_* D. 5 javascript sammy.js cordova
我和SammyJs建立了一个应用程序.它目前在浏览器中完美运行.但是,当我使用PhoneGap将其打包到Android时,路由不再起作用.
我发现了这个问题.但是,给出的解决方案不起作用:
(function($) {
var app = $.sammy('[role=main]', function() {
this.disable_push_state = true;
...
});
}
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过同样的问题?
编辑
我也使用jquery mobile和以下脚本来禁用它的路由:
<script type="text/javascript">
// DISABLE JQM ROUTER
$(document).bind("mobileinit", function () {
$.mobile.ajaxEnabled = false;
$.mobile.linkBindingEnabled = false;
$.mobile.hashListeningEnabled = false;
$.mobile.pushStateEnabled = false;
$.mobile.changePage.defaults.changeHash = false;
});
</script>
Run Code Online (Sandbox Code Playgroud)
我用我的app sammy javascript(包括路线)创建了一个要点.
我认为问题出在这个 around 子句上:
\n\nthis.around(function(callback) {\n var context = this;\n\n url = \'http://localhost:3000/api.json?school=\' + localStorage.school\n\n this.load(url)\n .then(function(data) {\n parsed = JSON.parse(data);\n\n //if (parsed.meta != undefined) {\n // alert(parsed.meta.message);\n //}\n context.products = parsed.products;\n context.places = parsed.places;\n context.school = parsed.school;\n context.title = $(\'[data-role=header] h1\');\n })\n .then(callback); // *** this won\'t get called if load() rejects promise\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n据我了解, around 子句是通过回调()来调用的,它在被调用时将继续加载路由。
\n\n我认为你的承诺链有问题。如果 load() 返回拒绝的 Promise(这可能会发生,因为您的手机上没有 localhost:3000),那么您的 then() 函数都不会加载。因此,回调()不会被调用并且应用程序“停止”。我建议(a)在那里添加一些错误处理,这样你就可以看到发生了什么,并且绝对(b)无论 load() 的结果如何都执行回调。另外 - 如果数据不是正确的 JSON 编码字符串,JSON.parse(data) 将抛出错误 - 您也需要尝试/捕获它。
\n\n我会尝试这个:
\n\nthis.load(url)\n.then(function(data) {\n try {\n parsed = JSON.parse(data);\n } catch(e) {\n console.log(\'error decoding json!: \'+errorMsg);\n }\n\n //if (parsed.meta != undefined) {\n // alert(parsed.meta.message);\n //}\n context.products = parsed.products;\n context.places = parsed.places;\n context.school = parsed.school;\n context.title = $(\'[data-role=header] h1\');\n},function(errorMsg){\n console.log(\'error loading json!: \'+errorMsg);\n})\n.fin(callback); // *** fin() is meant to execute on both success and error,\xc2\xa0like a "finally".\n
Run Code Online (Sandbox Code Playgroud)\n\n如果您的 Promise 实现不支持 fin(),请查找它调用其等效项的内容。它本质上是以下内容的简写:.then(callback).otherwise(callback)
长话短说 - 您希望确保传递给 around 的回调无论如何都会被执行,否则您的应用程序将不会继续加载路线,这就是您的意外行为。
\n\n至于看不到控制台的问题,我不确定你的环境是什么样的,但我过去使用 Eclipse 和 ADT 取得了成功 - 我可以很好地看到控制台日志和错误。
\n 归档时间: |
|
查看次数: |
449 次 |
最近记录: |