What is the best folder structure practice for Javascript / Backbone related to delegate helper functions?
-
I have the following Javascript folder structure: - js - libs - Backbone - Underscore - Require - Etc - models - templates - views - app.js - main.js - router.js In order to avoid cluttering the front end router with callback functions, ideally I want to delegate the functionality to external modules and have at maximum 1 line of code per route. This way I keep a very clean overview and I should never actually touch the router again when delegate functionality changes. For example: var Router = Backbone.Router.extend({ /* --- 1. Route management --- */ routes: { '': 'landing_page', '(/)login': 'auth_redirect', '(/)home': 'auth_redirect' }, landing_page: function(){ this.navigate("/login", {trigger:true}); }, auth_redirect: function(){ //Checks if the user is authenticated; //Returns either "true" or "false" $.get('/ingeb/api_v1/auth', _.bind(function(response){ var success = $.parseJSON(response)['success']; if (success === false){ this.renderView(Login_view); }else if(success === true){ this.renderView(Home_view); }; }, this)); }, ... I would like to delegate the code that handles the authentication check and redirection to an external module. I want to do the same for helper functions that I can call as static methods (no need to instantiate) throughout the entire application. Since my folder structure is very clean now, I would like to keep it this way. Is there any best practice to order these: Delegate objects; Helper function; in a clean folder structure ?
-
Answer:
I have applied the following tree structure that works really well for me: _controllers _models _views core helpers libs templates main.js app.js router.js Models, views and controllers are prefixed with underscore, for the simple fact that it is easy to find them back at the top of the structure. When multiple folders are expanded simultaneously, I found that this is of great help. Controllers are not native in Backbone. In my application, they are native Javascript objects that I use to manage the interaction between router, views and models. This helps to avoid cluttering the router/view objects with functionality. The core folder contains object that are 'core' to the application. Such as the user object, authentication object, application state object, etc. These object are not feature specific, and their main role is to keep track of a certain 'state' within the application and to execute related functionailty (such as authentication check when loading a page). Helpers are 'general' helper objects (not feature specific) that execute 'static' functions that are useful throughout the entire application. For example, a function that manages string or date parameters, a function that parses the response of an ajax call, a function that manages proper cleanup of view objects, etc... For what concerns views, controllers and templates, I take care that their child tree structure is identical, which makes it easy to find back any corresponding model / controller / template for a view immediately. For every view, there would be a native controller, implementing standard functions for that related view (eg. makeView() or getModel()). Generally, I tend to structure my views according to the place where they will be placed in the interface. Eg. header, subheader, leftnavbar, etc... I create subfolders below these main folders. Although this may not seem to be the best abstract solution to the problem, I found this very easy to manage. There may be controllers that don't fit 100% percent the view structure, but when using a dependency manager such as require.js, it is fairly easy to find back any file that you need. When working with a RESTful back end such as Laravel, I use a last folder that contains all PHP files. For example a folder named 'api_v1'. This means that all the routes to the back end are clearly separated from the front end routes.
Kim Gysen at Quora Visit the source
Related Q & A:
- What is the best practice for free space for a SQL server database drive?Best solution by Database Administrators
- What is the best camcorder for the best price?Best solution by Yahoo! Answers
- What is an electronic folder structure?Best solution by recordsmanagement.tab.com
- What is the best all around fishing rod size is best for bass?Best solution by ChaCha
- What is the electron dot structure?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.