0

i would like to navigate my nested view in backbone app with routes. I have next code:

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  */ 
  }

});

So, i need to show species when route is "item/:name/species/:speciesName/" but i get only triggering showSpecies fucntion, not both. What shall i do to trigger showItem and then showSpecies functions when route is "item/:name/species/:speciesName/" ?

Pavel Poberezhnyi
  • 733
  • 13
  • 28

1 Answers1

1

There is nothing brand new here. Simply call your showItem function directly from showSpecies.
Additionally, you could use routes hash instead of appRoutes and then it is possible to do so:

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

  routes: {
    'item/:name/' : 'showItem',
    'item/:name/species/:speciesName/' : function(){
      this.showItem();
      this.showSpecies();
    }
 }

});

James Akwuh
  • 2,169
  • 1
  • 23
  • 25
  • Thank u. But in my case this solution cause reloading item each time when i want to show another species. Is there a way to escape reloading item and show only proper species when /:speciesName/ changes? – Pavel Poberezhnyi Apr 01 '16 at 15:45
  • @PavelPoberezhnyi of course. You could save somewhere your current item and add check at the beginning of your `showItem` function: `if (currentItem === newItem) return false;` – James Akwuh Apr 01 '16 at 16:23