Code Monkey home page Code Monkey logo

cycle-vote's Introduction

cycle.js

Installation

$ yarn global add create-cycle-app

$ create-cycle-app my-awesome-app --flavor cycle-scripts-one-fits-all
> use typescript
> use most.js

Basic

import * as most from 'most'
import {Sources, Sinks} from './interfaces'

export function App(sources : Sources) : Sinks {
  const vtree$ = most.of(
    <div>My Awesome Cycle.js app</div>
  )

  return {
    DOM: vtree$
  }
}

BMI Calculator

import * as most from 'most'
import {Sources, Sinks} from './interfaces'

export function App(sources : Sources) : Sinks {
  const changeWeight$ = sources.DOM.select('.weight').events('input')
  .map(evt => evt.target.value)

  const changeHeight$ = sources.DOM.select('.height').events('input')
  .map(evt => evt.target.value)

  const state$ = most.combine(
    (weight, height) => {
      console.log(`weight=${weight}, height=${height}`)
      const heightMeters = height * 0.01
      const bmi = Math.round(weight / (heightMeters * heightMeters))
   
      return { bmi, weight, height }
    },
    changeWeight$.startWith(70),
    changeHeight$.startWith(100)
  )

  const vtree$ = state$.map(({ height, weight, bmi }) => 
    <div>
      <div><input type="text" className="height" value={height}/>Height</div>
      <div><input type="text" className="weight" value={weight}/>Weight</div>
      <div>
        <h1>BMI: {bmi}</h1>
      </div>
    </div>
  )

  return {
    DOM: vtree$
  }
}

Model-View-Intent

The same example as above, but refactored as MVI.

import * as most from 'most'

import {Sources, Sinks} from './interfaces'

// import Task from './component'


function intent (sources: Sources) {
  return {
    changeWeight$: sources.DOM.select('.weight')
      .events('input')
      .map(evt => evt.target.value),
    changeHeight$: sources.DOM.select('.height')
      .events('input')
      .map(evt => evt.target.value)
  }
}


function model (actions) {
  const weight$ = actions.changeWeight$.startWith(70)
  const height$ =  actions.changeHeight$.startWith(100)

  return most.combine(
    (weight: number, height: number) => {
      console.log(`weight=${weight}, height=${height}`)
      const heightMeters = height * 0.01
      const bmi = Math.round(weight / (heightMeters * heightMeters))
      return { bmi, weight, height }
    },
    weight$,
    height$
  )
}

function view (state$) {
  return state$.map(({ height, weight, bmi }) => 
    <div>
      <div><input type="text" className="height" value={height}/>Height</div>
      <div><input type="text" className="weight" value={weight}/>Weight</div>
      <div>
        <h1>BMI: {bmi}</h1>
      </div>
    </div>
  )
}


export function App(sources : Sources) : Sinks {

  const actions = intent(sources)
  const state$ = model(actions)
  const vtree$ = view(state$)

  return {
    DOM: vtree$
  }
}

Naming convention

// The $ sign indicates that the variable is a stream
const state$ = ...

cycle-vote's People

Contributors

alextanhongpin avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.