Top Banner
KEEPING THINGS SIMPLE IN A GROWING ANGULARJS APPLICATION 5 LESSONS LEARNED FROM 40K LINES OF JAVASCRIPT
47

Keeping Things Simple In A Growing AngularJS App.

Jul 16, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Keeping Things Simple In A Growing AngularJS App.

KEEPING THINGS SIMPLEIN A GROWING

ANGULARJS APPLICATION5 LESSONS LEARNED FROM 40K LINES OF JAVASCRIPT

Page 2: Keeping Things Simple In A Growing AngularJS App.

Brandon Boswell@BRANDONKBOSWELL

[email protected]

Page 3: Keeping Things Simple In A Growing AngularJS App.
Page 4: Keeping Things Simple In A Growing AngularJS App.

An authoring tool that enables doctors and clinical staff to illustrate the optimal way to treat any illness and share that knowledge with

those involved in care.

Page 5: Keeping Things Simple In A Growing AngularJS App.

How Do You Do That?

Page 6: Keeping Things Simple In A Growing AngularJS App.

Diagramming Tool+

Document Processor+

Collaboration, Workflow and Review Tools

Page 7: Keeping Things Simple In A Growing AngularJS App.
Page 8: Keeping Things Simple In A Growing AngularJS App.
Page 9: Keeping Things Simple In A Growing AngularJS App.

What's the stack?

Page 10: Keeping Things Simple In A Growing AngularJS App.

StackRails API

+AngularJS Client Application

+Sometimes a NodeWebkit Wrapper 1

1 IE8 Is Still Very Much Alive In Hospitals

Page 11: Keeping Things Simple In A Growing AngularJS App.

ALRIGHT BACK TO THE TIPS

Page 12: Keeping Things Simple In A Growing AngularJS App.

How To Stay Sane As An [Angular Developer | Developer | Human]?

Page 13: Keeping Things Simple In A Growing AngularJS App.

KEEP IT SIMPLE

Page 14: Keeping Things Simple In A Growing AngularJS App.

CLIENT APPS != SIMPLE

Page 15: Keeping Things Simple In A Growing AngularJS App.

UX != SIMPLE

Page 16: Keeping Things Simple In A Growing AngularJS App.

KEEP IT SIMPLE

Page 17: Keeping Things Simple In A Growing AngularJS App.

KEEP IT SIMPLEAt Least In Your Controllers

Page 18: Keeping Things Simple In A Growing AngularJS App.

LESSON 1:DON’T STORE ANYTHING USEFUL IN YOUR CONTROLLERS

Because You Will Probably Need Access To That Stuff

Page 19: Keeping Things Simple In A Growing AngularJS App.

Javascript angular.module('dorsata') .controller('pathwayCtrl', [ function(){ $scope.pathway = {}; } ]);

HTML <input type="html" ng-model="pathway.title">

Page 20: Keeping Things Simple In A Growing AngularJS App.

INJECT Data Objects USING SERVICES**SERVICES == FACTORIES == PROVIDERS.

Put Them All Together In Your App

Page 21: Keeping Things Simple In A Growing AngularJS App.

A REALLY SIMPLE DATA OBJECTangular.module('dorsata') .factory('currentPathway', [ function(){ return { "pathway": {} } }]);

Page 22: Keeping Things Simple In A Growing AngularJS App.

Javascriptangular.module('dorsata') .controller('pathwayCtrl', [ 'currentPathway', function(currentPathway){ // <--- Inject Object $scope.currentPathway = currentPathway; // <--- Bind It To Scope }]);

HTML<input type="html" ng-model="currentPathway.pathway.title">

Page 23: Keeping Things Simple In A Growing AngularJS App.

LESSON 2:SIMPLE CONTROLLERS, COMPLEX SERVICES.

Because You Will Probably Need To Handle Multiple Interactions

Page 24: Keeping Things Simple In A Growing AngularJS App.

CONTROLLERS ARE ONLY FOR handling user interaction.

Page 25: Keeping Things Simple In A Growing AngularJS App.

THINGS CONTROLLERS DO:1. HANDLE USER EVENT

2. DELEGATE TO SERVICE 3. INDICATE STATE TO USER

Page 26: Keeping Things Simple In A Growing AngularJS App.

Business Logic should live in services.

Page 27: Keeping Things Simple In A Growing AngularJS App.

EXAMPLE(Building On Previous Example)

Page 28: Keeping Things Simple In A Growing AngularJS App.

angular.module('dorsata') .controller('pathwayCtrl', [ 'currentPathway', 'pathwayRepo', function(currentPathway, pathwayRepo){ $scope.currentPathway = currentPathway;

// Handling The Event $scope.save = function(pathway){ //Setting Loading States $scope.loading = true; //Calling Service Code pathwayRepo.save(pathway) // Trigger Analytics, $http.put, Notify Collections Of Change .finally(function(){ $scope.loading = false; }); }; }]);

Page 29: Keeping Things Simple In A Growing AngularJS App.

LESSON 3:LEVERAGE PATTERNS THAT EMERGE

See Them, Use Them

Page 30: Keeping Things Simple In A Growing AngularJS App.

IF YOU FIND YOURSELF WRITING SIMILAR CODE MULTIPLE TIMES

Give it a name, and a folder.

Page 31: Keeping Things Simple In A Growing AngularJS App.

▸ Collections▸ Helpers▸ Listeners▸ ModelHelpers▸ Repositories▸ ViewModels

Page 32: Keeping Things Simple In A Growing AngularJS App.

LESSON 4:APPS CHANGE, UX CHANGES, CONTROLLERS CHANGE

Knowing What You Don't Know

Page 33: Keeping Things Simple In A Growing AngularJS App.

UX / DEVELOPMENT ARE ITERATIVE.

Page 34: Keeping Things Simple In A Growing AngularJS App.

We do not know what we want &

we especially don't know what the user/client wants

at the beginning.

Page 35: Keeping Things Simple In A Growing AngularJS App.

COMMON MISCONCEPTION:Everything should be a directive

Page 36: Keeping Things Simple In A Growing AngularJS App.

NOT EVERYTHING HAS TO BE A DIRECTIVE, especially initially

Page 37: Keeping Things Simple In A Growing AngularJS App.

Build more complex implementationsas your understanding of your user's needs increases.

Page 38: Keeping Things Simple In A Growing AngularJS App.

Why do you hate on directives?

Page 39: Keeping Things Simple In A Growing AngularJS App.

THINGS TO MAKE INTO DIRECTIVES

▸ Shared UI Components▸ Behavior/Interaction Patterns (Utility Directives)

Page 40: Keeping Things Simple In A Growing AngularJS App.

SHARED UI COMPONENTS AT DORSATA:▸ Sidebars▸ Menus▸ Menu-items

▸ Comment Boxes (Autocomplete)▸ User Avatars

Page 41: Keeping Things Simple In A Growing AngularJS App.

USER INTERACTION/UTILITY DIRECTIVESWhen the Page Loads,

I Want The Search Field Already Highlighted

Page 42: Keeping Things Simple In A Growing AngularJS App.

Let's Build That One.

Page 43: Keeping Things Simple In A Growing AngularJS App.

Javascriptangular.module('dorsata') .directive('focusOnLoad', function() { return function(scope, elem, attr) { elem[0].focus(); }; });

HTML<form> <input type="text" focus-on-load></input></form>

Page 44: Keeping Things Simple In A Growing AngularJS App.

EXAMPLES▸ Tab => onTab

▸ Shift+Tab => onShiftTab▸ Escape Key => onEscape▸ Ctrl+Enter => onCtrlEnter

▸ Focusing/Blurring Inputs => focusOn▸ Confirm-Clicks => confirmClick

Page 45: Keeping Things Simple In A Growing AngularJS App.

LESSON 5:TREAT YOUR ANGULAR CODE LIKE YOUR BACKEND CODE

It's Just As Important.

Page 46: Keeping Things Simple In A Growing AngularJS App.

AngularJS Production App Checklist:

[ ✓ ] - Error Tracking (Bugsnag) [ ✓ ] - Continuous Integration (Codeship)

[ ✓ ] - User Analytics (Mixpanel)

Page 47: Keeping Things Simple In A Growing AngularJS App.

QUESTIONS?