Code Organization in Large AngularJS and JavaScript Applications
cliffmeyers.com
cliffmeyers.com
I'm building a django/angular app. Each django app gets its own angular module, and each angular module roughly shares the seed structure, but may be broken into more files (much like the second example in TFA). So my whole structure looks something like:
django-project/
app1/
views.py
static/
coffeescript/
app1.coffee #defines the module
app1/
directives.coffee
controllers/
foobaz_controller.coffee
...
The only thing I'm not keen on is the duplication of "app1" in the directory structure. It is done to prevent conflicts when static files are collected, but is redundant during development.Thinking through this, I might be able to build a static files finder that looks for an angular directory in the django app and puts it in the right place. I'll have to think about that, since there are a lot of moving pieces with pipeline, et al, involved in the chain.
My one tip: forget about using Django to manage your assets if you need cache-busting... use fabric and something like grunt.
God forbid I try to edit multiple components at a time, and forget browsing my repo on github. Organization like this would make life so much easier in so many ways.
I find the general layout suggested for Angular apps to be a bit weird. There are prominent articles by Angular devs that suggest splitting things into services, models etc. I guess it just follows the familiar pattern that Rails etc give you.
I've recently built a couple of large(ish) python web apps. I use Flask for the web frontend but instead of starting with Flask I built all the libraries / core code first and then use Flask to tap into that. It makes for a much better application structure. Instead of thinking about how you should be splitting into blueprints you really think about the application structure as a whole (and you remove dependencies on the framework). I'm _much_ happier with the results.
I'm refactoring the Angular frontend at the moment so the example you've pointed to is a timely piece of code for me to skim. Thanks!
My applications are backbone based and my directory structure is similar to :
scripts/
classes/
collections/
socket.coffee
ui/
popup.coffee
page.coffee
models/
user.coffee
collections/
items.coffee
products.coffee
ui/
navigation/
top.coffee
sidebar.coffee
overlay.coffee
pages/
landing/
home.coffee
about.coffee
cms/
home.coffee
admin.coffee
utils/
templates.coffee
landing.coffee
cms.coffe
What's important in my app is classes path. I put there only reusable classes, and using coffeescript I can simply extend the whole module by class Items extend require('classes/collections/socket.coffee')
module.exports = new Items()
Also I always use utils folder, for general javascript helper functions. models/
CartModel.js
ProductModel.js
SearchResultsModel.js
UserModel.js
I have a lot of controllers, directives, filters and services which sit fine in their own files/modules but I don't understand what I would put in a model file.A good example of this is how the tutorial treats the restful 'Phone' resource, creating a service that injects the resource where necessary. I've started calling them 'models' rather than 'services' internally as well, so it's interesting to me that others are too.
These are then injected as "services" in your controllers.
Does anyone know if the Rails core team ever discussed using this directory layout? I know that it would take a fairly big rewrite of some glue logic in Rails that we all know and love, but merely 'hard to do' often does not stand in the way of 'the right way' in Rails.. right?
Can anyone point out something that immediately stands out as possibly problematic?
In my minds eye I don't see a nice place to stuff views. Module specific ones could live inside the module but Rails has layouts and also typically has shared views.
Sinatra would be a good framework to play around with this structure.
Using $rootScope feels like a hack.