-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.js
More file actions
50 lines (47 loc) · 1.23 KB
/
action.js
File metadata and controls
50 lines (47 loc) · 1.23 KB
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
console.log('>>>>>>>>>>>>>>> action>>>>>>>>>>>>>>>>');
var person = mobx.observable({
firstName: 'xiao',
lastName: 'mao',
age: 0,
get fullName() {
console.count('fullName');
return this.firstName + ' ' + this.lastName;
},
setAge: mobx.action(function(age) {
var self = this;
_.times(10, function() {
self.age = _.random(50);
});
this.age = age;
}),
setFirstName: mobx.action(function(firstName) {
this.firstName = firstName;
}),
setlastName: mobx.action(function(lastName) {
this.lastName = lastName;
}),
setFirstNameAndlastName: mobx.action(function(firstName, lastName) {
this.setFirstName(firstName);
this.setlastName(lastName);
})
});
mobx.autorun(function() {
console.log('autorun: ' + person.fullName + ' ' + person.age);
});
person.setAge(_.random(40));
person.setFirstNameAndlastName('john', 'smith');
person.firstName = 'firstName';
person.lastName = 'lastName';
//console result
/*
>>>>>>>>>>>>>>> action>>>>>>>>>>>>>>>>
fullName: 1
autorun: xiao mao 0
autorun: xiao mao 23
fullName: 2
autorun: john smith 23
fullName: 3
autorun: firstName smith 23
fullName: 4
autorun: firstName lastName 23
*/