【问题标题】:nested views with backbone routes带有主干路由的嵌套视图
【发布时间】:2016-07-21 11:26:43
【问题描述】:

我想在主干应用程序中使用路由导航我的嵌套视图。我有下一个代码:

var StoreRouter = Backbone.Marionette.AppRouter.extend({

  appRoutes: {
    'item/:name/' : 'showItem',
    "item/:name/species/:speciesName/" : "showSpecies"
  }

});

var StoreCtrl = Marionette.Object.extend({

  showItem: function(name){
    console.log("showItem");
    /* execute function for show Item */  
  },

  showSpecies: function(name, speciesName){
    console.log("showSpecies");
    /* execute function for show Species inside Item Layout  */ 
  }

});

所以,当路线为“item/:name/species/:speciesName/”时,我需要显示物种,但我只能触发 showSpecies 功能,而不是两者。当路由为“item/:name/species/:speciesName/”时,我应该怎么做才能触发 showItem 和 showSpecies 函数?

【问题讨论】:

    标签: javascript backbone.js routes marionette nested-routes


    【解决方案1】:

    这里没有什么全新的东西。只需直接从showSpecies 调用您的showItem 函数即可。
    此外,您可以使用routes 哈希而不是appRoutes,然后可以这样做:

    var StoreRouter = Backbone.Marionette.AppRouter.extend({
    
      routes: {
        'item/:name/' : 'showItem',
        'item/:name/species/:speciesName/' : function(){
          this.showItem();
          this.showSpecies();
        }
     }
    

    });

    【讨论】:

    • 谢谢你。但在我的情况下,每次我想展示另一个物种时,这个解决方案都会导致重新加载项目。有没有办法在 /:speciesName/ 更改时避免重新加载项目并仅显示正确的物种?
    • @PavelPoberezhnyi 当然。您可以将当前项目保存在某处并在 showItem 函数的开头添加检查:if (currentItem === newItem) return false;
    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多