Sammyjs路由不适用于Phonegap

Sammyjs routes not working with Phonegap

本文关键字:Phonegap 适用于 不适用 路由 Sammyjs      更新时间:2023-09-26

我用SammyJs构建了一个应用程序。它目前在浏览器中运行良好。然而,当我使用PhoneGap将其打包到安卓系统时,这些路由不再有效。

我发现了这个SO问题。然而,给出的解决方案不起作用:

(function($) {
    var app = $.sammy('[role=main]', function() {
      this.disable_push_state = true;
      ...
    });
}

有人经历过同样的问题吗?

编辑

我也使用jquery移动与以下脚本禁用其路由:

 <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>

我用我的应用程序sammy javascript(包括路由)创建了一个要点。

我认为问题出在这个around子句上:

this.around(function(callback) {
  var context = this;
  url = 'http://localhost:3000/api.json?school=' + localStorage.school
  this.load(url)
    .then(function(data) {
      parsed = JSON.parse(data);
      //if (parsed.meta != undefined) {
      //  alert(parsed.meta.message);
      //}
      context.products = parsed.products;
      context.places = parsed.places;
      context.school = parsed.school;
      context.title = $('[data-role=header] h1');
    })
    .then(callback); // *** this won't get called if load() rejects promise
});

据我所知,around子句是用callback()调用的,它将在调用路由时继续加载路由。

我认为你的承诺链有问题。如果load()返回一个被拒绝的promise(可能是这样,因为你的手机上没有localhost:3000),那么你的then()函数都不会加载。因此,callback()不会被调用,应用程序也会"停止"。我建议(a)在那里添加一些错误处理,这样你就可以看到它发生了什么,当然(b)无论load()的结果如何都执行回调。此外,如果数据不是一个正确的JSON编码字符串,JSON.parse(data)将抛出一个错误——您也希望对此进行尝试/捕获。

我会试试这个:

this.load(url)
.then(function(data) {
  try {
     parsed = JSON.parse(data);
  } catch(e) {
     console.log('error decoding json!: '+errorMsg);
  }
  //if (parsed.meta != undefined) {
  //  alert(parsed.meta.message);
  //}
  context.products = parsed.products;
  context.places = parsed.places;
  context.school = parsed.school;
  context.title = $('[data-role=header] h1');
},function(errorMsg){
  console.log('error loading json!: '+errorMsg);
})
.fin(callback); // *** fin() is meant to execute on both success and error, like a "finally".

如果您的promise实现不支持fin(),请查找它所称的等价物。它本质上是.then(callback).otherwise(callback) 的简写

长话短说-你想确保传递给周围的回调无论如何都会被执行,或者你的应用程序不会继续加载路由,这就是你的意外行为。

至于不能看到控制台的问题,我不确定您的环境是什么样子的,但我过去在Eclipse和ADT方面取得了成功——我可以很好地看到控制台日志和错误。