I'm working on my first large front-end application built with Marionette.js framework. By now I have module BlogApp, which is small blog sub-appliaction with only 2 methods list(to list all posts) and show (to display single post by id):
@MainApp.module "BlogApplication", (BlogApplication, App, Backbone, Marionette, $, _) ->
class BlogApplication.Router extends Marionette.AppRouter
appRoutes:
"blog" : "list"
"posts/:id" : "showPost"
API =
list: ->
BlogApplication.List.Controller.list()
showPost: (id, post) ->
BlogApplication.Show.Controller.showPost id, post
App.addInitializer ->
new BlogApplication.Router
controller: API
App.vent.on "blog:post:clicked", (post) ->
App.navigate Routes.post_path(post.id)
API.showPost post.id, post
App.on "blog:list": ->
App.navigate("blog")
API.list()
My folders are organized like this:
--blog
-blog_app.js
--list
--templates
-blog_sidebar.template
-blog_panel.template
-blog_layout.template
-blog_post.template
-list_controller.js
-list_view.js
--show
--templates
-blog_sidebar.template
-blog_panel.template
-blog_layout.template
-blog_post.template
-show_controller.js
-show_view.js
Everything works fine.
The only difference between the show and list pages - is displaying one full post instead of listing them all. Now I have 2 controllers and I'm duplicating my templates for show
and list
methods, which is totally not a good practice. But I also don't think that I should implement my show
method in List.Controller
- it will help me to use same templates, because it will break my application infrastructure and in future, when I have to add some features, for example - comments,tags etc, it will mess up everything. How should I organize my blog module the right way?
The way I organise my code has been working overall well without any memory leaks (afaik) until now.
I will share this with you and for any people who might find this useful.
I will also provide some supplementary information on how I manage the memory.
1) For the folder structure I have something like:
In my application, the
app_blog
is an app (folder) that uses an app_name.js file to manage the routes, and bootstrap the subapps, and controllers to manage subsections of the app.When a route is hit:
A controller in my application has the following format:
Then when another route gets triggered and another app is started, I will call
app.trigger("reset")
if necessary to close the controller and unbind events that were bound by the controller.So far, this has worked well for me.