This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://unpkg.com/mobx@2.6.0/lib/mobx.umd.js"></script> | |
<script> | |
var MobxDemo = Object.create(HTMLElement.prototype); | |
MobxDemo.attachedCallback = function() { | |
var state = mobx.observable({ | |
counter : parseInt(this.getAttribute("counter")) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -- app.js -- | |
import { AbstractNode } from './internal' | |
/* as is */ | |
// -- internal.js -- | |
export * from './AbstractNode' | |
export * from './Node' | |
export * from './Leaf' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parameterized computed views: | |
// Create computed's and store them in a cache | |
import { observable, computed } from "mobx" | |
class Todos { | |
@observable todos = [] | |
private todosByUserCache = new Map() | |
getAllTodosByUser(userId) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
<link rel="stylesheet" type="text/css" href="./styles.css"> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
<!-- All of the Node.js APIs are available in this renderer process. --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
@observable firstName = "Michel"; | |
@observable lastName = "Weststrate"; | |
@observable nickName; | |
@computed get fullName() { | |
return this.firstName + " " + this.lastName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const byId = (state, action) => | |
produce(state, draft => { | |
switch (action.type) { | |
case RECEIVE_PRODUCTS: | |
action.products.forEach(product => { | |
draft[product.id] = product | |
}) | |
break | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The state of our app | |
var state = mobx.observable({ | |
nrOfSeats : 500, | |
reservations : [], | |
seatsLeft : function() { return this.nrOfSeats - this.reservations.length; } | |
}); | |
// The UI; a function that is applied to the state | |
var ui = mobx.computed(function() { | |
return "\n<div>Seats left: " + state.seatsLeft + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { decorate, observable, flow, onBecomeObserved, onBecomeUnobserved } from 'mobx' | |
import { log } from './log' | |
const APPID = '<secret>' | |
export class City { | |
location | |
temperature | |
interval |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {observable} from "mobx" | |
import {observer} from "mobx-react" | |
@observer class Select extends React.Component { | |
@observable selection = null; /* MobX managed instance state */ | |
constructor(props, context) { | |
super(props, context) | |
this.selection = props.values[0] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// case 2: manipulates the value of the descriptor, returns fresh descriptor | |
class MyClass { | |
@action someMethod() { | |
} | |
} | |
function action(target, key, descriptor) { | |
const baseValue = descriptor.value | |
return { |
NewerOlder