Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js如何根据特定的应用程序状态定义自定义路由集_Javascript_Backbone.js_Url Routing - Fatal编程技术网

Javascript js如何根据特定的应用程序状态定义自定义路由集

Javascript js如何根据特定的应用程序状态定义自定义路由集,javascript,backbone.js,url-routing,Javascript,Backbone.js,Url Routing,目前正在构建一个在手机上运行的应用程序 与当前问题无关,但通过某个事件 该应用程序处于在线或离线状态(无论是否可通过手机访问互联网) 离线应用程序非常有限,只有几个屏幕可用) 现在阻止我如果你发现我做了傻事或者我可以做得更好的事, 但我的第一个想法是让路由器有一组动态的路由 就像您可以在集合上定义动态url属性一样 因此,与此相反: var router = Backbone.Router.extend({ routes: { '' : 'homeAction',

目前正在构建一个在手机上运行的应用程序 与当前问题无关,但通过某个事件 该应用程序处于在线或离线状态(无论是否可通过手机访问互联网)

离线应用程序非常有限,只有几个屏幕可用)

现在阻止我如果你发现我做了傻事或者我可以做得更好的事, 但我的第一个想法是让路由器有一组动态的路由

就像您可以在集合上定义动态url属性一样

因此,与此相反:

var router = Backbone.Router.extend({
    routes: {
        '' : 'homeAction',
        'categories' : 'categoriesAction',
        ...
    },
    homeAction: function(){ },
    categoriesAction: function(){ }
});
我在想:

var router = Backbone.Router.extend({
    initialize: function(){
        this.on('app:togglestate', this.toggleRoutes, this);
    },
    toggleRoutes: function () {
        var router = this;
        if(App.onlineModus)
            router.routes = { /* hash with online routes here */ };
        else
            router.routes = { /* hash with offline routes here */ };
    },
    routes: {
        '' : 'homeAction',
        'categories' : 'categoriesAction',
        ...
    },
    homeAction: function(){ },
    categoriesAction: function(){ }
});
虽然这会彻底破坏整个应用程序, 作为
主干.history.start()引发错误,无法从未定义调用函数start。
这让我相信routes对象在初始化时以某种方式被使用,不能动态更改

我可能想去很远的地方吗? 我应该以其他方式实现这一点吗

我的另一个想法是:

  • 路由与url完全相同,其中routes参数是返回哈希的函数,这也不起作用
  • 现在我的想法完全不同了,测试应用程序在每一条路线的动作中是在线还是离线。虽然这看起来太复杂了,但我可能不得不通过一个动作来传递它们,只有在离线MODU中可以访问路由的情况下,这个动作才会传递到实际动作?但如果不编写太多的模板代码,我真的不知道如何从这样的中继动作开始

    • 我不得不做一些非常类似的事情。我面前没有代码,但这应该是我所做的:(编辑:充实它一点,以便您现在可以实际运行它)


      从那时起,当我需要访问应用程序路由器时,我只引用了ApplicationRouter.instance()

      为了动态更新路由,您需要在更新路由后调用_bindrouts()

      例如:

      toggleRoutes: function () {
          var router = this;
          if(App.onlineModus)
              router.routes = { /* hash with online routes here */ };
          else
              router.routes = { /* hash with offline routes here */ };
      
          // Get rid of previous navigation history
          if(Backbone.history){
              Backbone.history == null;
          }
      
          // Bind the new routes
          router._bindRoutes();
      }
      

      请注意,动态更改管线时,历史记录不再有效,因此需要删除以前的历史记录。当调用_bindrouts时,它会自动实例化一个新的主干.history,当调用this.route时。

      我也很担心,所以我需要查看另外两个选项中的一个,要么通过网关操作传递所有操作以首先通过此测试,要么我需要切换两个路由器对象。能否在应用程序执行期间动态卸载路由器?我想我会先试试这个。这是一个有效的例子,虽然有点像黑客,因为你调用了一个n u函数,它应该将它标记为私有函数。它不应该被直接调用。虽然你是对的,但如果你不介意使用这个黑客,它是一个完美的工作解决方案。谢谢你的回答!
      toggleRoutes: function () {
          var router = this;
          if(App.onlineModus)
              router.routes = { /* hash with online routes here */ };
          else
              router.routes = { /* hash with offline routes here */ };
      
          // Get rid of previous navigation history
          if(Backbone.history){
              Backbone.history == null;
          }
      
          // Bind the new routes
          router._bindRoutes();
      }