-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
113 lines (107 loc) · 2.9 KB
/
demo.html
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.1.js" type="text/javascript"></script>
<script src="unimodel.js" type="text/javascript"></script>
<style type="text/css">
</style>
</head>
<body>
<ul id="view"></ul>
<script type="text/javascript">
$(document).ready(function () {
var $view = $('#view');
var model = new Unimodel({
override:{
'state.lock': function(value, field, action) {
var readonly = (action == 'get');
if (!readonly) {
console.log(field + "- action '" + action + "' is locked, new value '" + value + "' rejected");
}
return readonly;
},
'person': function(value, field, action) {
var me = this;
if (action == 'merge') {
var result = $.Deferred();
setTimeout(function() {
result.resolve({sex: '---'});
}, 3000);
return result;
} else {
return true;
}
}
},
data: {
counts: {
total: 50,
avrg: 10
},
welcome: 'hello',
person: {
name: 'paul',
tmp: '111'
},
groups: [
{id: 1},
{id: 2}
],
state: {
lock: 'readonly'
}
},
events: {
'update': function(field, value, promiseData) {
$view.append('<li>event update: ' + field + '</li>');
console.log('update event: field="', field, '", value="', this.get(field), '"', promiseData !== undefined ? ' async' : '');
},
'merge': function(field, value, promiseData) {
$view.append('<li>event merge: ' + field + '</li>');
console.log('merge event: field="', field, '", value="', value, '", result="', this.get(field), '"', promiseData !== undefined ? ' async' : '');
},
'remove': function(field, value, promiseData) {
$view.append('<li>event remove: ' + field + '</li>');
console.log('remove: ' + field, '", value="', value, '"', promiseData !== undefined ? ' async' : '');
}
}
});
console.log('original model: ', $.extend(true, {}, model.get()));
model.get('state.lock');
console.log('get: field="state.lock", value="', model.get('state.lock'), '"');
setTimeout(function() {
model.set('counts.total', 100);
model.set({
'state.unlock': 'create and write access',
'state.lock': 'try to write'
});
}, 2000);
setTimeout(function() {
model.merge('welcome', ' world');
model.merge('state.lock', 'try to merge');
model.merge('counts.avrg', 7);
model.merge('counts.total', 2, function(oldValue, newValue) {
return oldValue * newValue;
});
model.merge('person', {sex: 'M'}, {});
model.merge('groups', [
{id: 3}
], []);
model.merge('groups', [
{id: 2},
{id: 3},
{id: 4},
{id: 5}
], [], 'id');
}, 4000);
setTimeout(function() {
model.remove(['state.lock', 'groups.4', 'person.tmp']);
}, 6000);
setTimeout(function() {
model.remove('state');
console.log('final model: ', $.extend(true, {}, model.get()));
}, 8000);
});
</script>
</body>
</html>