start: function() {
// This should really be in its own file. You're creating a router, controller,
// and more all inside your application module, which leads to a single monolithic
// structure that's much harder to maintain. Since it doesn't look like you're
// using AMD or other dependency injection, the namespace scheme that you currently
// have should be sufficient. If you use CapsCamelCase for constructors and
// lowerCamelCase for instances, you can even save references to both using
// your namespace object under (mostly) the same name.
MW.catalog_controller = {
// These methods are the PRIMARY place logic should be placed for when a route
// is matched. Any listeners for `navigate` events should only be performing
// SECONDARY logic, like closing UI elements that are no longer needed. Basically,
// you should move the logic on your `on` methods directly into these router callbacks.
// If for some reason you
index: function() {
console.log("index");
},
showCategory: function(permalink) {
console.log("showCategory");
},
showProdcutDetail: function(slug) {
console.log("showProdcutDetail");
}
}
// Again, this should really be in its own file for modularity's sake
MW.appRouter = new MW.Router({
controller: MW.catalog_controller,
appRoutes: {
'': 'index',
"t/:permalink": "showCategory",
"products/:slug": "showProdcutDetail"
}
});
Backbone.history.start({pushState: true});
// Again, move into its own file. If there's only ever going to be one instance
// of this view on screen at a time, that file should return an INSTANCE of the
// view, rather than its constructor.
MW.root_view = new MW.View.RootView({
el: '#primary-container',
regions: {
mobile_nav: "#mobile-nav",
desktop_nav: "#desktop-nav",
page_content: "#page-content",
first_footer: "#first-footer",
second_footer: "#second-footer"
}
});
// this should probably be in the initialize method of your root view
MW.main_vew = new MW.View.MainNav({el: ".header-primary" })
// And I'd put this call in the `onShow` method of the root view
MW.root_view.getRegion("desktop_nav").show(MW.main_vew);
},
// These methods are pretty much unnecessary. You should move their logic into
// the router callbacks defined in your controller.
onNavigateCategory: function (dest) {
// The router handles this part automatically. You don't need to call it as long
// as the logic is in the controller methods, and you shouldn't need a reference
// to the router in the controller itself.
MW.appRouter.navigate(dest, {trigger: true});
// Same comments as with the root view. If you don't actually need multiple
// instances of this view, you should be using an instance reference created
// directly after the view definition. You don't want to create a whole new
// instance every time the route is triggered - that way lies memory leaks.
MW.category_view = new MW.View.CategoryPageView({
destination: dest
});
// Move this to the `onShow` method of the category_view
MW.root_view.getRegion("page_content").show(MW.category_view);
},
onNavigateHome: function (dest) {
// Same comments as above
MW.appRouter.navigate(dest, {trigger: true});
MW.home_view = new MW.View.HomePageView({
});
MW.root_view.getRegion("page_content").show(MW.home_view);
},
onNavigateProductDetail: function (dest) {
// Same comments as above.
console.log("onNavigateProductDetail");
MW.appRouter.navigate(dest, {trigger: true});
MW.pdp_view = new MW.View.ProductDetailPageView({
});
MW.root_view.getRegion("page_content").show(MW.pdp_view);
}
});