-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controller.js
executable file
·207 lines (186 loc) · 6.07 KB
/
Controller.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* A Singleton <code>Controller</code> implementation.
*
* <P>
* In PureMVC, the <code>Controller</code> class follows the 'Command and Controller' strategy, and assumes
* these responsibilities:
*
* <UL>
* <LI>
* Remembering which <code>Command</code>s are intended to handle which <code>Notifications</code>.
* </LI>
*
* <LI>
* Registering itself as an <code>Observer</code> with the <code>View</code> for each
* <code>Notification</code> that it has an <code>Command</code> mapping for.
* </LI>
*
* <LI>
* Creating a new instance of the proper <code>Command</code> to handle a given <code>Notification</code>
* when notified by the <code>View</code>.
* </LI>
*
* <LI>
* Calling the <code>Command</code>'s <code>execute</code> method, passing in the <code>Notification</code>.
* </LI>
* </UL>
* </P>
*
* <P>
* Your application must register <code>Command</code>s with the Controller.
* </P>
*
* <P>
* The simplest way is to subclass <code>Facade</code>, and use its <code>_initializeController</code> method
* to add your registrations.
* </P>
*
* @see org.puremvc.js.core.View
* @see org.puremvc.js.patterns.observer.Observer
* @see org.puremvc.js.patterns.observer.Notification
* @see org.puremvc.js.patterns.command.SimpleCommand
* @see org.puremvc.js.patterns.command.MacroCommand
*/
Object.declare('org.puremvc.js.core.Controller');
/**
* Constructor.
*
* <P>
* This <code>Controller</code> implementation is a Singleton, so you should not call the constructor
* directly, but instead call the static Singleton Factory method <code>Controller.getInstance()</code>
* </P>
*
* @throws Error Error if Singleton instance has already been constructed
*/
org.puremvc.js.core.Controller = function ()
{
if (org.puremvc.js.core.Controller.instance !== null) {
throw new Error(org.puremvc.js.core.Controller.SINGLETON_MESSAGE);
}
org.puremvc.js.core.Controller.instance = this;
this._commandMap = {};
this._initializeController();
};
// Singleton instance
org.puremvc.js.core.Controller.instance = null;
// Contant messages
org.puremvc.js.core.Controller.SINGLETON_MESSAGE = 'Controller Singleton already constructed!';
/**
* <code>Controller</code> Singleton Factory method.
*
* @return Controller the Singleton instance of <code>Controller</code>
*/
org.puremvc.js.core.Controller.getInstance = function ()
{
if (org.puremvc.js.core.Controller.instance === null) {
org.puremvc.js.core.Controller.instance = new org.puremvc.js.core.Controller();
}
return org.puremvc.js.core.Controller.instance;
};
var _p = org.puremvc.js.core.Controller.prototype;
// Mapping of Notification names to Command Class references
_p._commandMap = null;
// Local reference to View
_p._view = null;
/**
* Initialize the Singleton <code>Controller</code> instance.
*
* <P>Called automatically by the constructor.</P>
*
* <P>Note that if you are using a subclass of <code>View</code>
* in your application, you should <i>also</i> subclass <code>Controller</code>
* and override the <code>_initializeController</code> method in the
* following way:</P>
*
* <listing>
* // ensure that the Controller is talking to my View implementation
* _p._initializeController = function ()
* {
* this._view = MyView.getInstance();
* };
* </listing>
*
* @return void
*/
_p._initializeController = function ()
{
this._view = org.puremvc.js.core.View.getInstance();
};
/**
* If an <code>Command</code> has previously been registered to handle a the given
* <code>Notification</code>, then it is executed.
*
* @param notification <code>Notification</code>
*
* @return void
*/
_p.executeCommand = function (notification)
{
var notificationName = notification.getName(),
commandClassRef,
commandInstance;
if ( ! this.hasCommand(notificationName)) {
return;
}
commandClassRef = this._commandMap[notificationName],
commandInstance = new commandClassRef();
commandInstance.execute(notification);
};
/**
* Register a particular <code>Command</code> class as the handler for a particular
* <code>Notification</code>.
*
* <P>
* If an <code>Command</code> has already been registered to handle <code>Notification</code>s
* with this name, it is no longer used, the new <code>Command</code> is used instead.
* </P>
*
* <P>
* The Observer for the new ICommand is only created if this the first time an ICommand has been
* regisered for this Notification name.
* </P>
*
* @param notificationName the name of the <code>Notification</code>
* @param commandClassRef the <code>Class</code> of the <code>Command</code>
*
* @return void
*/
_p.registerCommand = function (notificationName, commandClassRef)
{
if ( ! this.hasCommand(notificationName)) {
this._view.registerObserver(
notificationName,
new org.puremvc.js.patterns.observer.Observer(this.executeCommand, this)
);
}
this._commandMap[notificationName] = commandClassRef;
};
/**
* Check if a Command is registered for a given Notification
*
* @param notificationName
*
* @return boolean Whether a Command is currently registered for the given <code>notificationName</code>.
*/
_p.hasCommand = function (notificationName)
{
return (typeof this._commandMap[notificationName] !== 'undefined');
};
/**
* Remove a previously registered <code>Command</code> to <code>Notification</code> mapping.
*
* @param notificationName The name of the <code>Notification</code> to remove the <code>Command</code> mapping for.
*
* @return void
*/
_p.removeCommand = function (notificationName)
{
// If the Command is registered...
if (this.hasCommand(notificationName)) {
// Remove the observer
this._view.removeObserver(notificationName, this);
// Remove the command
this._commandMap[notificationName] = null;
delete this._commandMap[notificationName];
}
};