Component-based Frontend Architecture Zaiyang, Steve, Alex, Dave
What are Our Goals?
● Discuss frontend architecture ● Frameworks? ● Why Component-based architecture is good ● Angular’s approach to components ● Good JavaScript tools and libraries
Traditional MVC (n-tiered) Architecture Tries to be Loosely Coupled ● Separation of concern ● App is divided into layers: Model, View, Controller, Service, Persistence, Networking, etc.
But MVC has lots of disadvantages: Complexity, Fragility, Non-reusable. Difficult to extend existing functionality
MVC is DEAD FAREWELL TO MVC
Component- based Frameworks Ember.js Vue.js Angular 2 React.js Polymer
What is a component? A component is a self-contained object which owns its own presentation logic, view, and internal state
Example: Button<button>
A Component owns its state
A Component knows how to render itself View = template + state
A Component is reusable They can be composed <ul> <li><button>hello</button></li> </ul>
function HeaderController($scope, $element, $attrs){ //... } angular.module(‘myApp’) .component(‘header’,{ template: `<h1>{{ header.title }}</h1>`, controller: HeaderController, controllerAs: ‘header’ bindings: { ‘title’: ‘@’ } }); //…. // <header title=”Pet Shop”></header> Angular’s Component Approach
One-way Data Flow Model
Input Field Value = ‘hello’ Parent Child.onchange <- parent.textChanged parent.onchange(newValue) Value = ‘hello’ textChanged() Child.value <- parent.value
Input Field value=’hello’ Parent Parent.value = child.value value=’hello’ $scope.$watch(...)
Don’t use two-way data binding
Avoid Two-way data binding as much as possible Two-way databinding creates tight coupling between components Angular watchers creates performance issues ~ upper limit 2000
What about Application state? Services? Controller? View? X Maybe Maybe
What about Application state? Components! Container Component Presentation Component
Centralized Application Store
Other Architectural paradigms Reactive (Rx) Flux/Redux Functional

Component based architecture