-
Notifications
You must be signed in to change notification settings - Fork 6
/
watch.js
57 lines (45 loc) · 1.33 KB
/
watch.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
/**
* @file san composition api demo
*/
import {
defineComponent,
template,
data,
method,
watch
} from '../index';
const wrapper = document.createElement('div');
document.body.appendChild(wrapper);
const App = defineComponent(context => {
template(`
<div>
<div><strong>Watch Function</strong></div>
<span>name: {{name}}</span>
<button on-click="rename">rename</button>
<br ><br >
<span>company: {{company}}</span>
<button on-click="hop">hop</button>
</div>
`);
const info = data('company', 'baidu');
watch('company', function (value, e) {
const company = info.get();
console.log(`data changes, new company: ${company}, val: ${value}`);
console.log({e});
});
method('hop', () => {
info.set('baidu~' + Math.random());
});
const nameData = data('name', 'myName');
method('rename', function () {
context.data.set('name', 'jinz~' + Math.random());
// info.set('name', 'jinz~' + Math.random());
});
watch('name', function (value, e) {
const name = nameData.get();
console.log(`data changes, got new name: ${name}`);
});
}, san);
let myComponent = new App();
myComponent.data.set('company', 'iqiyi');
myComponent.attach(wrapper);