Essentially I have an app Layout with a common header and footer section for a particular route. The thing is I want to be able to transition the main content region for this layout when a sub-route has been hit.
Example: #products being the main route and #products/:id being the sub route.
In my controller for this module I am using require.js to grab the view for the #products route and show the landing with the global header and footer as part of this layout's regions. I am also defining a content region, which is the one I want to transition out once an id is included in the route. So how can I call methods on the view once this new route has been hit? Would I need to cache the current state of the application when the parent route is hit and then reference this when the sub-route is hit? And would I also need to init the view when the sub-route is hit and the parent route has not been visited by the user?
Router
define(['backbone', 'marionette', 'c_controllers/Controller'], function ( Backbone, Marionette, Controller ) {
'use strict';
var AppRouter = Backbone.Marionette.AppRouter.extend({
appRoutes : {
// PRODUCT ROUTES
'product' : 'product',
'product/:id' : 'showPlp'
}
});
return new AppRouter({'controller': Controller});
});
Controller
define(["backbone", 'marionette'], function (Backbone, Marionette) {
'use strict';
return {
product : function( id ) {
require(['c_product/product',
'app_views/menu'], function( Product, Menu ) {
APP.menu.show( new Menu() );
APP.page.show( new Product() );
});
}
};
});
View
define([
'backbone', 'marionette', 'app_views/globalNav',
'c_product/productLanding', 'text!productNavTemplate', 'text!productBaseTemplate'], function( Backbone, Marionette, GlobalNav, ProductLanding, ProductNavTemplate, ProductBaseTemplate ) {
var product = Backbone.Marionette.Layout.extend({
id : 'product',
template : _.template( ProductBaseTemplate ),
regions : {
nav : '#globalNav',
content : '#view-content',
footer : '#footer'
},
events : {
},
initialize : function() {
},
onRender : function() {
// extend the nav view for the template that matches this flow
var Nav = GlobalNav.extend({ template : _.template( ProductNavTemplate )});
// show the nav, main content, and footer
this.nav.show( new Nav() );
this.content.show( new ProductLanding() );
}
});
return product;
});