I am currently working on a large web application based on backbone.js and you have a lot of problems with organization, zombies, etc., so I decided to do the main code refactoring. I already wrote a bunch of helper functions for working with "zombies"; however, I would like to start from the very beginning and create a good structure / organization for the code. I did not find many great tutorials / examples for large-scale backbone.js organization, so I started from scratch and would like to know if I can get some opinions on where I started.
I obviously set up my code in a global namespace; but I would also like to keep this namespace pretty clean. My main app.js keeps class files separate from the global namespace; you can register a class (so that it can be created) using the reg () function, and the inst () function creates an instance of the class from an array of classes. Thus, in addition to 3 methods, the MyApp namespace has only Router, Model and View:
var MyApp = (function () { var classes = { Routers: {}, Collections: {}, Models: {}, Views: {} }; methods = { init: function () { MyApp.Router = MyApp.inst('Routers', 'App'); MyApp.Model = MyApp.inst('Models', 'App'); MyApp.View = MyApp.inst('Views', 'App'); Backbone.history.start(); }, reg: function (type, name, C) { classes[type][name] = C; }, inst: function (type, C, attrs) { return new classes[type][C](attrs || {}); } }; return methods; }()); $(MyApp.init);
In models, collections, routers and views, I work as usual, but then you need to register this class at the end of the file so that it can be created at a later point (without cluttering the namespace) with
MyApp.reg('Models', 'App', Model);
Does this sound like an unnecessary way to organize code? Others have examples of how to organize really big projects with many routers, collections, models and views?
user527480 Nov 03 '11 at 10:40 2011-11-03 22:40
source share