1 of 18

Implementing unidirectional user interface in legacy project

Tushar Jarhad (@tjarhad)�Software Engineer @ Red Panda Innovation Labs

2 of 18

KOKO Networks �Cleanstar Ventures, East Africa

Launchpad for startups

3 of 18

Challenge

  • Project had ~150 server side rendered pages
  • New 3 Pages to be develop with strong user interaction

4 of 18

5 of 18

6 of 18

React

Problem of build integration with backend code

7 of 18

8 of 18

Snabbdom

  • Virtual DOM library with ~ 200 SLOC
  • Extendable through modules
  • Vue.js use a fork of snabbdom

9 of 18

View

function View ( {model, handler}){

return h('div.page',

{ style: fadeInOutStyle },

[

h('div',

{ style: counter},

model.counter)

])

}

10 of 18

JSX

import { html } from 'snabbdom-jsx';

function View ( {model, handler}){

return

<div>

<div selector=".counter"> {model.counter}</div>

</div>

}

11 of 18

Intent

<button

selector=".plus"

dataset={({value: "1"})}

on-click={(e) => handler({type:'add', payload:e})}>

Increment</button>

12 of 18

Action Update

const R = require('ramda');�function actionUpdate(model, action) {

let actionType = action.type;

switch(actionType){

case 'add':

return R.assoc('counter', model.counter + 1, model);

case 'subtract':

return R.assoc('counter', model.counter - 1, model);

default: return model

}

}

13 of 18

Action Handler & Patch

function updateUI() {

const newVnode = <View model={model} handler={handler}/>

vnode = patch(vnode, newVnode);

}

function handler(action) {

model = actionUpdate(model, action)

updateUI();

}

14 of 18

Libraries

  • Snabbdom
  • Snabbdom-jsx
  • Ramdajs
  • Rxjs or mostjs (optional)

15 of 18

Building with Flask Application

  • Babel for JSX to plain javascript transformation
  • Flask assets with browserify

16 of 18

References

17 of 18

Questions?

18 of 18

THANK YOU!