-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogic.js
95 lines (79 loc) · 1.96 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
//runInAction // https://www.youtube.com/watch?v=uAlxod75FIM
const { observable, observe, autorun, computed, runInAction } = mobx;
/*
class Todo {
// id = Math.random();
@observable title;
@observable finished = false;
@computed get unfinishedTodoCount() {
return this.finished;
}
constructor(title) {
this.title = title;
}
}*/
var todoStore = observable({
/* some observable state */
todos: [],
/* a derived value */
get completedCount() {
return this.todos.filter(todo => todo.completed).length;
}
});
/* a function that observes the state */
autorun(function() {
console.log("Completed %d of %d items",
todoStore.completedCount,
todoStore.todos.length
);
});
/* ..and some actions that modify the state */
todoStore.todos[0] = {
title: "Take a walk",
completed: false
};
// -> synchronously prints 'Completed 0 of 1 items'
todoStore.todos[0].completed = true;
// -> synchronously prints 'Completed 1 of 1 items'
(()=>{
const appState = observable({
get fullName () {
console.count('fullName');
return this.firstName + ' ' + this.lastName;
},
firstName: 'Matt',
lastName: 'Ruby',
form:'',
top:0,
devices:["d1","d2"],
age: 34,
temperature:80,
todos:[
{ title:"foo", done: false },
{ title:"bar", done: true },
],
get completedCount() {
console.count('completedCount');
return this.todos.filter(todo => todo.done).length;
}
})
/*
const appState = new class appState{
@observable firstName = 'Matt'
@observable lastName = 'Ruby'
@observable top =0
@observable devices =["d1","d2"]
@observable age = 34
@observable temperature=80
@observable todos=[
{ title:"foo", done: false },
{ title:"bar", done: true },
],
@computed get completedCount() {
console.count('completedCount');
return this.todos.filter(todo => todo.done).length;
}
}*/
window.appState = appState;
})()