46

I have seen several AngularJS project templates: the seed project at the official website, Yeoman's generated, and AngularFun.

Are there any other (un)opinionated templates I should take a look at, or any related pattern you would suggest for a scalable AngularJS project?

By scalable I mean

  • being able to split controllers, directives, filters, etc. in their own files;
  • being able to load these files on demand rather than making the browser load everything;
  • being able to have common, cross-project components (e.g. common directives, filters or services).
luisfarzati
  • 8,649
  • 6
  • 29
  • 27

6 Answers6

30

You can take a look at a demo application that Pawel Kozlowski and I are putting together: https://github.com/angular-app/angular-app.

It doesn't provide any support for loading files on demand but you can see we spit modules up into separate files and set up testing as a first class component. We have a build process (using Grunt) that concatenates (and minifies on release) the js files and can run unit and end to end tests.

We have chosen to split our modules into two groups functional application areas and common cross cutting library code, rather than a simple split into directives, filter, services and so on. In side a functional area we might have some service, directives, controllers and templates.

This makes developing against a functional area easier as all the relevant items are in one place.

The project relies on a simple nodeJS server to deliver the files (supporting HTML5 mode deep linking) and also to provide authentication and authorization services.

joragupra
  • 692
  • 1
  • 12
  • 23
Pete BD
  • 10,151
  • 3
  • 31
  • 30
  • 1
    As an additional comment: load on demand is not really well supported in AngularJS at the moment as explained here: http://stackoverflow.com/a/12646328/1418796 – pkozlowski.opensource Nov 23 '12 at 16:23
  • 1
    Your project has been incredibly useful, thanks! I liked the way you split the modules. I'm using Yeoman (based on Grunt) and I'm still dealing with a couple of issues regarding the build process, but overall I'm closing to achieve a nice first attempt on this! – luisfarzati Nov 24 '12 at 18:48
8

I would say that all of your points can be easly achived, at least without any modifications to Angular.

  • being able to split controllers, directives, filters, etc. in their own files;

this can be of course done with basic Angular, as you can include as many script tags with controllers/services as you want. Of course it's not scalable at all, so the best option would be using AMD modules, like RequireJS. This is one of the seeds that have this kind of configuration: https://github.com/elsom25/angular-requirejs-html5boilerplate-seed

  • being able to load these files on demand rather than making the browser load everything;

As pkozlowski suggested in the comments, there is already and entry with some description of the problem, you will see that I was also working to solve this, and actually had some results. I have a working example of loading controllers, templates and directives on demand using RequireJS and the resolve param of route configuration.

  • being able to have common, cross-project components (e.g. common directives, filters or services)

Having the previous points resolved it could be easily achived using RequireJs modules.


I've been wondering whether starting an agularjs-lazy-seed project would be a good idea? Is there any demand for that? We could even take it further and move the routes configurations outside the usual configuration, let's say you have a views.json file (ideally a service that responds with json) with the views you want to include in your application:

{
    "views" : {
        ....
        "account" : {             
             "path" : "/account" // route path
             "name" : "Account", // view name
             "partial" : "views/account/account.html", // partial file
             "controller" : "account/main" // RequireJS module
             "directives" : [ "directives/version", "directives/menu" ] // directives used in the view
        }
        ....
    }
}

This way you could:

  • develop the views in separation and the build the application based on this json bootstrap
  • have some common directives and components
  • when bootstrap after the login you could filter the views that user is allowed to see
  • everything inside the ngView would be loaded on demand

Of course your application should then be really big so that doing all of this additional work would made sense ;)

matys84pl
  • 8,344
  • 1
  • 18
  • 18
  • Actually I wanted to add some of these as a comment to what pkozlowski wrote in the previous answer but I'm not yet allowed to put comments everywhere;) – matys84pl Nov 23 '12 at 19:38
  • could you post something more info about on-demand load of directives with AngularJS? As far as I know it is not possible without really nasty tricks. – pkozlowski.opensource Nov 23 '12 at 21:33
  • Sure, I've managed to do it by holding the reference of $compileProvider until I get the directive module loaded, the code is here: https://github.com/matys84pl/angularjs-requirejs-lazy-controllers/blob/master/app/js/utils/lazy-directives.js . Also check out the route-congif.js - this is where I use it. I'm loading all 3 files (partial, controller and directives) in parallel. – matys84pl Nov 23 '12 at 22:12
  • Very complete answer, thank you! It seems that I should read more about RequireJS. I don't think my project is *that* big yet as to involve this complexity, but I think I'll have to revisit this in the mid-term. :) – luisfarzati Nov 24 '12 at 18:58
  • FWIW, I'd like to see on-demand loading of views/components just for the "what user is allowed to see" case). So yes, if you have time, code up a sample... – kenyee Aug 27 '13 at 21:21
8

You should try ng-boilerplate. The most promising kickstart template for bigger AngularJS projects: http://joshdmiller.github.io/ng-boilerplate/#/home

ottsch
  • 431
  • 1
  • 6
  • 14
4

I agree with the points other folks have said so far; its very easy to split things up into separate modules and have the modules depend on each other with regular AngularJS stuff. Then your JS code can be split into whatever files and directory trees you prefer.

Just thought I'd mention what we're doing in the open source hawtio project which is based on AngularJS. We've taken modularity to a bit of an extreme :) hawtio uses plugins which can be discovered at runtime in the running server (e.g. deploy and undeploy UI functions at runtime). So based on some REST query or JMX detection we can dynamically and or remove plugins.

e.g. here are all our current default plugins

In terms of layout, each plugin has its own directories for code (js), html partials (html) and anything else (e.g. css / img directories) which makes it easy to keep things nice and modular. e.g. here's the camel plugin which has its own html, js and img folders.

Then a specific plugin defines its own AngularJS module, directives, filters and which can then depend on other modules.

We've not come up with terribly many helpful naming conventions for the source files so far though :). We find writing a file per controller seems simplest; but other than the fooPlugin.ts file and the helpers.ts file (for general module specific helper functions) we've not yet found any other meaningful naming conventions so far.

James Strachan
  • 9,168
  • 34
  • 31
1

This project sounds promising http://vesparny.github.io/ng-kickstart

It makes you able to split your codebase by feature and keep your code reusable, as well as livereloading thanks to a custom Grunt task for that.

The project is also unit testing oriented and comes with a custom "dist task" that let you to create an optimized, production ready release.

1

Warning: Shameless plug.

You should definitely check out generator-angular-xl.

It aims especially at creating large scale AngularJS applications by grouping code logically, scaffolding unit tests, and automatically injecting your js and css files into index.html etc. It also helps out by creating a mock back-end for your data, effectively making it a good choice when developing prototypes that also can live into becoming full blown applications. It does NOT generate any back-end code, so you are free to choose whatever back-end technology you want.

Kenneth Lynne
  • 15,461
  • 12
  • 63
  • 79