On Github JuanCaicedo / elm-to-js-slides
var mainLoop = require('main-loop'); var vdom = require('virtual-dom'); var render = function(state) { // Code for rendering a view }; var initialState = { title: 'test' }; var loop = mainLoop(initialState, render, vdom); document.querySelector('#content').appendChild(loop.target);
loop.update({ title: 'new title' });
render state = h1 [] [ text state.title ]
var hh = require('hyperscript-helpers'); var h1 = hh.h1; var render = function(state){ h1('.title', state.title); };
/* Initial state */ var initialState = { selectedLegislators: [{ firstName: 'Juan', lastName: 'Caicedo' }, { firstName: 'Foo', lastName: 'Bar' }], availableLegislators: [{ firstName: 'Senator', lastName: 'One' }, { firstName: 'Congresswoman', lastName: 'Two' }] };
var render = function (state) { return div('.container', [ legislatorTableView('Your Team', state.selectedLegislators), legislatorTableView('Available', state.availableLegislators) ]); };
var legislatorTableView = function (title, legislators) { return div('.col-xs-6', [ h1(title), table('.table.table-striped', [ tbody( R.map(legislatorView, legislators) ) ]) ]); };
var legislatorView = function (legislator) { return tr('.container-fluid', [ td('.col-xs-6', legislator.firstName), td('.col-xs-6', legislator.lastName) ]); });
var action = function(type, data) { return { type: type, data: data }; };
var emitter = new EventEmitter();
function address(action) { emitter.emit('update', action); };
emitter.on('update', function(action) { var newState = update(loop.state, action); loop.update(newState); });
var update = function (state, action) { var newSelected; var newAvailable; // fallback case var newState = state; if (action.type === 'Drop') { // move from Selected to Available } else if (action.type === 'Select') { // move from Available to Selected } return newState; };
newSelected = R.append(action.data, state.selectedLegislators); newAvailable = R.reject(R.equals(action.data), state.availableLegislators); newState = R.merge(state, { selectedLegislators: newSelected, availableLegislators: newAvailable });
var render = R.curry(function (address, state) { return div('.container', [ legislatorTableView(address, 'Drop', 'Your Team', state.selectedLegislators), legislatorTableView(address, 'Select', 'Available', state.availableLegislators) ]); });
var loop = mainLoop(initialState, render(address), vdom);
var legislatorTableView = function (address, type, title, legislators) { return div('.col-xs-6', [ h1(title), table('.table.table-striped', [ tbody( R.map(legislatorView(address, type), legislators) ) ]) ]); };
var legislatorView = R.curry(function (address, type, legislator) { return tr('.container-fluid', { onclick: function(ev) { address(action(type, legislator)); } }, [ td('.col-xs-6', legislator.firstName), td('.col-xs-6', legislator.lastName) ]); });