background preloader

Code Organization in Large AngularJS and JavaScript Applications — Cliff Meyers

Code Organization in Large AngularJS and JavaScript Applications — Cliff Meyers
Many developers struggle with how to organize an application's code base once it grows in size. I've seen this recently in AngularJS and JavaScript applications but historically it's been a problem across all technologies including many Java and Flex apps I've worked on in the past. The general trend is an obsession with organizing things by type. It bears a striking resemblance to the way people organize their clothing. Let's take a look at angular-seed, the official starting point for AngularJS apps. css/img/js/ app.jscontrollers.jsdirectives.jsfilters.jsservices.jslib/partials/ The JavaScript directory has one file for every type of object we write. This is a mess. The next logical pass at organizing JavaScript involves creating a directory for some of the archetypes and splitting objects into their own files. Let's imagine we're building a simple e-commerce site with a login flow, product catalog and shopping cart UI's. Nice!

I Wish I Knew Then What I Know Now -- Life With AngularJS AngularJS is a powerful if stubborn Javascript framework. What follows is simply a list of things that I wish were clearer to me when I started working with AngularJS. Hopefully, someone just starting with AngularJS will find this list helpful. Data Binded Directives Manipulate the data, not the DOM. Additionally, you can write your own custom directives to manipulate a particular DOM element and add functionality to it. In short, when you start with AngularJS, you need to make a conscious effort to forget what you thought you knew about manipulating the DOM with Javascript. Services are just global objects AngularJS wires in services to your controllers for you, in an attempt at implementing dependency injection in Javascript. Embrace broadcast messaging Any reasonably complex view will end up containing multiple controllers. Which all leads us to… Combine broadcast messaging with services The pattern that we eventually fell into was to combine broadcast messaging with services.

Angular Structure: Refactoring for Growth Your Angular app is growing and you want your structure to adapt with it. Let’s explore one way you can approach this. As your app grows it becomes even more important to structure it in a way that makes it easy to manage and maintain it. I recently posted a common technique I use when I create a new app to help structure it. A simple structure by type is easy out of the gate, as the app generally starts with a single set of features. This usually starts as the first module you write. Options for Managing Growth I like the type organization (folders for controllers and views, for example), but as an app grows, instead of putting that at the root and putting all views and controllers in their own folders, sometimes it makes more sense to organize them 1 level deeper into feature areas. So how might this look? Sort By Type On the left we have the app organized by type. Sort By Feature On the left the project is organized by feature. Structuring for Modules Your Choice Like this: Like Loading...

AngularJS tips and tricks for neophytes and the all-around curious developer. – bytes for thought Article written as of AngularJS 1.0.7/1.1.5. Forget jQuery’s event handling altogether. I know it’s hard at first, but the temptation to use jQuery is the first and most common mistake developers make when starting out with Angular. It is so deeply engrained in our minds that you must force yourself to perform a paradigm shift. Since the entire application now has much fewer physical entry points (i.e. Angular’s handling of the DOM will inevitably clash with that of jQuery, unless handled properly, and that is, through «Directives». Luckily for us, however, others have gone through the hassle and wrapped common widgets and librairies for us, under the AngularUI toolkit. But… where is Angular’s “DOMready” event, if I can’t use jQuery’s $(document).ready()? That’s the tricky part. Using $timeout’s in general in Javascript isn’t exactly elegant, but until there is a better way to handle it, it does the trick. $timeout(function(){console.log("DOM is ready! Avoid the $scope. $scope.

AngularJS: an Overview - Glenn Stovall AngularJS is a JavaScript framework made by Google for building complex client-side applications. Angular’s killer feature is ‘directives’ which allow you to extend HTML by creating your own tags and attributes. Angular projects have a somewhat different structure than other JavaScript MVC frameworks, but it can be highly modular and easy to maintain once you understand the structure. The Philosophy of AngularJS Data First If you are familiar with JavaScript libraries like jQuery, using AngularJS will require a bit of a paradigm shift. Highly Testable AngularJS was designed to be testable end-to-end. Declarative HTML Angular is designed to extend HTML to make it the language you need for building complex web applications. Data Binding Let’s take a look at a very simple AngularJS app, that uses two-way data binding. Let’s break this down line by line to explain what is happening: <body ng-app> : all Angular code must be wrapper in this directive. Modules Controllers Directives Services Routing

Angular Modules and RequireJS best practices? Well I'm as blind as a bat with this stuff right now but if I can make progress I'll certainly post it for you! I'm not doing ETE testing yet – trying to plug in unit tests first. I think I got past the problem my gist is referring to and have a new one (the problem I was having was due to the fact that someone (ME) forgot to add the service.js.coffee file to the karma config file… once I did that I got past that part of it. Now I'm having a problem with the inject code… Basically I have no idea what I need to put in there or how to do it… I have a controller that has this signature: @DsetCtrl = ($scope, $resource, dimResource) -> I have a service that looks a bit like this: angular.module "Naph", ["ngResource", "$strap.directives", "ng-rails-csrf", "EitDirectives", "EitServices"] angular.module("EitServices", []).factory "dimResource", ($resource, $rootScope) -> (obviously a bunch of code truncated out and I'm just showing the signatures of both) $httpBackend = _$httpBackend_; at null. at null.

Authentication in Single Page Applications with Angular.js - A Modest Proposal 9/2/2014 update:The GitHub repo now uses UI-router instead of the standard ngRoute routing module. This blog post will still illustrate the original technique utilizing ngRoute, while information regarding the UI-router approach can be found in the GitHub repo as well as this blogpost. The example app in the repo has been deployed to Heroku, so now you can test it out live for yourself right here. A separate blogpost discussing the complementing server-side code can be found here. I have been working a lot with Angular.js lately, and love how easy it makes it to create web applications with rich client-side functionality. How do I deal with authentication and authorization in single page applications? Since only the initial load of the app is a full page load, and subsequent communication with the server is entirely done via XHRs I need a slightly different model from the traditional one. The server needs to communicate what permissions/role the client has on inital page load.

Scenario Test Runner: expect(1).toEqual(1) should pass The scenario runner works in two passes. First it executes your it() function and builds a list of commands. Then it executes your commands waiting between them when one of them might be asynchronous. That way your test looks synchronous because you don't have to handle the callbacks yourself. count = element('body').count(); expect(count).toEqual(1) is a fancy way of doing: var futureCount = this.addFutureAction('count elements', function($window, $document, done) { // the runner waits for done to be called before executing the next command. // done takes two arguments (error, value). done(null, $document.find('body').length); this.addFuture('assert count', function(done) { // The scenario runner guarantees that by the time this callback is run the // previous command has completed and the value property has the // the result of the command. if (futureCount.value ! done('Expected 1 but was ' + futureCount.value); // Success so don't pass anything. done(); - show quoted text -

How does Angular JS relate to Google Closure? - Super Answer Posted on 06-20 by Polan (798 votes, average: 4.8 out of 5) Now that AngularJS 1.0 is released I am wondering how this project fits together with the other general-purpose JavaScript framework / tool from Google, Closure. I have only seen basic description of those two technologies (and read about a half of the book on Closure) so I have no direct experience but this is how it looks to me: Closure is a set of technologies that can be used separately. So these two technologies seem to be aimed at quite a different level of abstraction so my first thought was, can they be used together? "In that case, there would be no reason for them to get performance enhancing drugs that are illegal." Best Answers: How does Angular JS relate to Google Closure? The only Google project I'm aware of that uses AngularJS is the DoubleClick team. Google Closure Library comes with a UI framework in its goog.ui namespace. Speaking of classes and subclasses, Google Closure Library also has this. Related Articles:

Related: