Andrei Preda
Software engineer in love with JavaScript
What is it about?
Part of architecture: Choosing Technology and following software engineering laws and best practices
What cannot be changed
JavaScript v. 5
IE8/9?
+
The nice part
Maintainability
Small units of code
unit tested
Reusability of components
Easy to understand code, simplicity
Traceability of the system
Determine Input & Output for each unit
things that you can choose
Writing Javascript vs transpiling into browser supported Javascript (ECMAScript 5)
from The Register Jan 2014 (long time ago)
Now in summer 2015 ...
Transpiled programming:
The programming language
All try to solve some problems or to enhance JavaScript v. 5 to be better:
ES6 (chosen) technology + infrastructure
Start with only what it's needed and add as it goes! (no yeoman needed)
Deciding the infrastructure specific around ECMAScript 6:
what we need:
+
Deciding the larger infrastructure tool
This tool save time and provide a better life to the developer :) !
Start setup
NPM: 1. package.json
npm init
package.json contains both development modules and devDevelopment modules used to build the application
create packge.json:
eslint (supports es6 linting)
touch .eslintrc
npm install eslint -g
{
"env":{...}
"ecmaFeatures": {...},
"rules":{...}
}
rules can be airbnb style but let's start simple
{
"env": {
"es6": true
}
"ecmaFeatures": {
"modules": true
}
}
es6!
touch mocha.opts
--require babel/register
es6!
4. to run example unit test from npm:
#file: package.json
#considering a test directory exists where the
#package.json is located with a spec inside
"scripts": {
"test": "mocha --reporter list"
},
1. setup with babel to transpile the code into javascript v. 5
5. run test
#from the console
npm test
2.install babel module
npm install babel --save-dev
npm install mocha chai --save-dev
3.install mocha & chai modules
what is chai? assetion library that support assert, expect and should and works amazing with mocha
Requirement: build a basic example of a shopping cart application!
What do we need to start?
Choose the tools to enable us to obtain reusability, maintainability and focus on what we need to deliver!
1. reusable components
could be simple handlebars templates, or ES6 template strings
or isomorphic virtual dom diffing frameworks depending on the project complexity
let's go for something cool!
component
Input:
data
we don't care at this point where the data comes from: could be server calls could be localstorage, user interaction
Output:
html markup
could be svg, mobile app tags something that can be rendered and displayed!
The idea is the same:
2. states: components communication
start minimalistic ! (only what you need now)
flux / MVC introduce complexity and the architecture can get locked into a certain technology / framework that we don't need from the start
describe the states and use a central data structure as a channel of communication between components:
Start with baobab.js
How do we start? Time to code
The System
basket component
productList component
{data}
baobab
shoppingCart service
file/directory structure
The entry point
app.js!
'use strict';
import React from 'react';
//react components
import BasketComponent from './components/Basket.js';
import ProductListComponent from './components/ProductList.js';
React.render(<BasketComponent />, document.getElementById('basket'));
React.render(<ProductListComponent />, document.getElementById('product'));
es6!
The Component
basket
component
data are basket items: getInitialState method actions to remove an item: removeItemFromBasket custom method listeners when the component should re-render/update state componentDidMount method
The service
Contains the additional processing of data and manipulates the central data structure
linked with the data structure by baobab cursors
let shoppingCart = {
addItem (item) {
cursors.basketItems.push(item);
},
removeItem (item) {
cursors.basketItems.splice([this.items().indexOf(item), 1]);
},
items () {
return cursors.basketItems.get();
}
};
Outputs clear and unit tested methods which components will rely on
shoppingCart service
The State
The heart of the application
===
Amazing baobab library
It is a data tree! Let's start with one and try to keep only one instance unless the application turns crazy in complexity
Why not react-baobab ? why not react + baobab + flux?
They are awesome libraries but let's start simple, and you might realise you might not need them or if the application grows in complexity they are easy to add in as mixins or a dispatcher
Conclusion
Understand your code! Make others understand it!
By Andrei Preda