-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatcher.js
More file actions
62 lines (54 loc) · 1.23 KB
/
watcher.js
File metadata and controls
62 lines (54 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
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Author: Major
* Date: 11/30 2011
*/
if (!window.watcher) window.watcher = {};
(function(w) {
w.watchQueue=[];
/**
* @value {JSON} {attribute:attr,viewModel:viewModel,fct:fct,callback:callback}
*/
w.addWatchTarget=function(obj){
this.watchQueue.push(obj);
};
w.removeWatchTarget=function(key){
//todo
};
w.startWatch=function(){
for(var i=0;i<w.watchQueue.length;i++){
var obj=w.watchQueue[i];
w.playback(obj);
}
if(this.timeoutId) window.clearTimeout(this.timeoutId);
this.timeoutId=window.setTimeout(w.startWatch,0);
};
//use set time, currently could not stop watch, bug or not?
w.stopWatch=function(){
if(this.timeoutId){
window.clearTimeout(this.timeoutId);
}
};
w.playback=function(playbackObj){
var p=playbackObj;
var attribute=p.attribute;
var viewModel=p.viewModel;
var fct=p.fct;
var callback=p.callback;
if(fct.apply(viewModel,[attribute])){
callback();
}
};
// how to delete watch target?
// use UID as store key?
// how to watch object which has too much level?
w.watchUtil=function(attr,viewModel,fct){
return {
'do':function(f){
w.addWatchTarget({attribute:attr,viewModel:viewModel,fct:fct,callback:f});
if(fct.apply(viewModel,[attr])){
f();
}
}
};
};
})(watcher);