git ssb

1+

Matt McKegg / mutant



Commit c01dae98743153d400531ac256fb74b5ca35138e

add when transform

Matt McKegg committed on 7/8/2016, 3:46:32 AM
Parent: c2921b35648957f8ee09a7df19d9df07911c9160

Files changed

README.mdchanged
test/dom.jschanged
when.jsadded
README.mdView
@@ -15,18 +15,22 @@
1515
1616 ## Use
1717
1818 ```js
19 +var h = require('@mmckegg/mutant/html-element')
1920 var Struct = require('@mmckegg/mutant/struct')
2021 var send = require('@mmckegg/mutant/send')
21-var h = require('@mmckegg/mutant/html-element')
22 +var computed = require('@mmckegg/mutant/computed')
23 +var when = require('@mmckegg/mutant/when')
2224
2325 var state = Struct({
2426 text: 'Test',
2527 color: 'red',
2628 value: 0
2729 })
2830
31 +var isBlue = computed([state.color], color => color === 'blue')
32 +
2933 var element = h('div.cool', {
3034 classList: ['cool', state.text],
3135 style: {
3236 'background-color': state.color
@@ -35,11 +39,17 @@
3539 h('div', [
3640 state.text, ' ', state.value, ' ', h('strong', 'test')
3741 ]),
3842 h('div', [
39- h('button', {
40- 'ev-click': send(state.color.set, 'blue')
41- }, 'Change color')
43 + when(isBlue,
44 + h('button', {
45 + 'ev-click': send(state.color.set, 'red')
46 + }, 'Change color to red'),
47 + h('button', {
48 + 'ev-click': send(state.color.set, 'blue')
49 + }, 'Change color to blue')
50 + )
51 +
4252 ])
4353 ])
4454
4555 setTimeout(function () {
test/dom.jsView
@@ -1,14 +1,18 @@
11 var Struct = require('../struct')
22 var send = require('../send')
33 var h = require('../html-element')
4 +var computed = require('../computed')
5 +var when = require('../when')
46
57 var state = Struct({
68 text: 'Test',
79 color: 'red',
810 value: 0
911 })
1012
13 +var isBlue = computed([state.color], color => color === 'blue')
14 +
1115 var element = h('div.cool', {
1216 classList: ['cool', state.text],
1317 style: {
1418 'background-color': state.color
@@ -17,11 +21,17 @@
1721 h('div', [
1822 state.text, ' ', state.value, ' ', h('strong', 'test')
1923 ]),
2024 h('div', [
21- h('button', {
22- 'ev-click': send(state.color.set, 'blue')
23- }, 'Change color')
25 + when(isBlue,
26 + h('button', {
27 + 'ev-click': send(state.color.set, 'red')
28 + }, 'Change color to red'),
29 + h('button', {
30 + 'ev-click': send(state.color.set, 'blue')
31 + }, 'Change color to blue')
32 + )
33 +
2434 ])
2535 ])
2636
2737 setTimeout(function () {
when.jsView
@@ -1,0 +1,11 @@
1 +var computed = require('./computed')
2 +
3 +module.exports = When
4 +
5 +function When (obs, ifTrue, ifFalse) {
6 + return computed([obs, ifTrue, ifFalse], lambda)
7 +}
8 +
9 +function lambda (value, ifTrue, ifFalse) {
10 + return value ? ifTrue : ifFalse
11 +}

Built with git-ssb-web