-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappState.StateObject.js
136 lines (106 loc) · 3.41 KB
/
appState.StateObject.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/// <reference path="appState.utils.js" />
(function(appState, $, undefined) {
appState.StateObject = function(stateName) {
if(typeof(stateName) !== "string" || !stateName){
throw new Error("StateObject could not be initialized because the stateName was not a string or was empty.");
}
//------ Private Members
var self = this;
var parentState = stateName;
var initQueue = [];
var exitQueue = [];
//------ Public Members
this.getStateName = function() {
return parentState;
};
var invokeQueue = function (list, data) {
for (var i = 0; i < list.length; i++) {
var item = list[i];
if (typeof (item) === 'function') {
item(self, data);
}
}
};
//Called by State Monitor when the state is first changed to.
this.stateEnter = function (data) {
invokeQueue(initQueue, data);
};
//Called by State Monitor when the state is exited.
//This can be called whether the change was graceful or not!
this.stateExit = function() {
invokeQueue(exitQueue);
};
this.registerInitCallback = function(callback) {
appState.utils.registerCallback(initQueue, callback);
};
this.registerExitCallback = function(callback) {
appState.utils.registerCallback(exitQueue, callback);
};
this.deregisterInitCallback = function(callback) {
appState.utils.deregisterCallback(initQueue, callback);
};
this.deregisterExitCallback = function(callback) {
appState.utils.deregisterCallback(exitQueue, callback);
};
this.clearInitCallbacks = function() {
initQueue.splice(0, initQueue.length);
};
this.clearExitCallbacks = function() {
exitQueue.splice(0, exitQueue.length);
};
};
}(window.appState = window.appState || {}));
/* Samples
var sampleState = new appState.StateObject("sample");
var hiCallback = function(state) { alert("hi from " + state.getStateName() + "!");};
var echoLine = function(message) {
document.write(message + "<br/>");
};
sampleState.registerInitCallback(hiCallback);
sampleState.init();
echoLine("sampleState.getStateName():");
echoLine(sampleState.getStateName());
sampleState.deregisterInitCallback(hiCallback);
sampleState.init();
echoLine("sampleState.canChangeToState('bad!')");
if(sampleState.canChangeToState("bad!")){
echoLine("Let's do it!");
}
else {
echoLine("No Way!");
}
(function(dude, undefined) {
var baseState = new appState.StateObject("dudeState");
var stateObj = function() {
this.customProperty = "yo!";
this.canChangeToState = function(state) {
if(state !== "broState") {
return false;
}
return true;
};
};
stateObj.prototype = baseState;
dude.dudeState = new stateObj();
}(window.dude = window.dude || {}));
dude.dudeState.registerInitCallback(hiCallback);
dude.dudeState.init();
echoLine("<strong>dude.dudeState.getStateName():</strong>");
echoLine(dude.dudeState.getStateName());
dude.dudeState.deregisterInitCallback(hiCallback);
dude.dudeState.init();
echoLine("<strong>dude.dudeState.canChangeToState('bad!')</strong>");
if(dude.dudeState.canChangeToState("bad!")){
echoLine("Let's do it!");
}
else {
echoLine("No Way!");
}
echoLine("<strong>dude.dudeState.canChangeToState('broState')</strong>");
if(dude.dudeState.canChangeToState("broState")){
echoLine("Let's do it!");
}
else {
echoLine("No Way!");
}
*/