1+ /** 
2+  * @author  PureMVC JS Native Port by David Foley, Frédéric Saunier, & Alain Duchesneau  
3+  * @author  Copyright(c) 2006-2012 Futurescale, Inc., Some rights reserved. 
4+  *  
5+  * @class  puremvc.Controller 
6+  *  
7+  * In PureMVC, the Controller class follows the 'Command and Controller'  
8+  * strategy, and assumes these responsibilities: 
9+  *  
10+  * - Remembering which 
11+  * {@link  puremvc.SimpleCommand SimpleCommand}s 
12+  * or  
13+  * {@link  puremvc.MacroCommand MacroCommand}s 
14+  * are intended to handle which  
15+  * {@link  puremvc.Notification Notification}s 
16+  * - Registering itself as an  
17+  * {@link  puremvc.Observer Observer} with 
18+  * the {@link  puremvc.View View} for each  
19+  * {@link  puremvc.Notification Notification} 
20+  * that it has an  
21+  * {@link  puremvc.SimpleCommand SimpleCommand}  
22+  * or {@link  puremvc.MacroCommand MacroCommand}  
23+  * mapping for. 
24+  * - Creating a new instance of the proper  
25+  * {@link  puremvc.SimpleCommand SimpleCommand}s 
26+  * or  
27+  * {@link  puremvc.MacroCommand MacroCommand}s 
28+  * to handle a given  
29+  * {@link  puremvc.Notification Notification}  
30+  * when notified by the 
31+  * {@link  puremvc.View View}. 
32+  * - Calling the command's execute method, passing in the  
33+  * {@link  puremvc.Notification Notification}. 
34+  * 
35+  * Your application must register  
36+  * {@link  puremvc.SimpleCommand SimpleCommand}s 
37+  * or {@link  puremvc.MacroCommand MacroCommand}s  
38+  * with the Controller. 
39+  * 
40+  * The simplest way is to subclass  
41+  * {@link  puremvc.Facade Facade}, 
42+  * and use its  
43+  * {@link  puremvc.Facade#initializeController initializeController}  
44+  * method to add your registrations. 
45+  * 
46+  * @constructor  
47+  * This Controller implementation is a Multiton, so you should not call the  
48+  * constructor directly, but instead call the static #getInstance factory method,  
49+  * passing the unique key for this instance to it. 
50+  * @param  {string } key 
51+  * @throws  {Error } 
52+  *  If instance for this Multiton key has already been constructed 
53+  */ 
54+ function  Controller ( key ) 
55+ { 
56+     if ( Controller . instanceMap [ key ]  !=  null ) 
57+     { 
58+         throw  new  Error ( Controller . MULTITON_MSG ) ; 
59+     } 
60+ 
61+     this . multitonKey =  key ; 
62+     Controller . instanceMap [ this . multitonKey ] =  this ; 
63+     this . commandMap =  new  Array ( ) ; 
64+     this . initializeController ( ) ; 
65+ } 
66+ 
67+ /** 
68+  * @protected  
69+  *  
70+  * Initialize the multiton Controller instance. 
71+  * 
72+  * Called automatically by the constructor. 
73+  * 
74+  * Note that if you are using a subclass of View 
75+  * in your application, you should *also* subclass Controller 
76+  * and override the initializeController method in the 
77+  * following way. 
78+  *  
79+  *     MyController.prototype.initializeController= function () 
80+  *     { 
81+  *         this.view= MyView.getInstance(this.multitonKey); 
82+  *     }; 
83+  *  
84+  * @return  {void } 
85+  */ 
86+ Controller . prototype . initializeController =  function ( ) 
87+ { 
88+     this . view =  View . getInstance ( this . multitonKey ) ; 
89+ } ; 
90+ 
91+ /** 
92+  * The Controllers multiton factory method. 
93+  * 
94+  * @param  {string } key 
95+  *  A Controller's multiton key 
96+  * @return  {puremvc.Controller } 
97+  *  the Multiton instance of Controller 
98+  */ 
99+ Controller . getInstance =  function ( key ) 
100+ { 
101+     if ( null  ==  this . instanceMap [ key ] ) 
102+     { 
103+         this . instanceMap [ key ] =  new  this ( key ) ; 
104+     } 
105+ 
106+     return  this . instanceMap [ key ] ; 
107+ } ; 
108+ 
109+ /** 
110+  * If a SimpleCommand or MacroCommand has previously been registered to handle 
111+  * the given Notification then it is executed. 
112+  * 
113+  * @param  {puremvc.Notification } note 
114+  * @return  {void } 
115+  */ 
116+ Controller . prototype . executeCommand =  function ( note ) 
117+ { 
118+     var  commandClassRef =  this . commandMap [ note . getName ( ) ] ; 
119+     if ( commandClassRef  ==  null ) 
120+         return ; 
121+ 
122+     var  commandInstance =  new  commandClassRef ( ) ; 
123+     commandInstance . initializeNotifier ( this . multitonKey ) ; 
124+     commandInstance . execute ( note ) ; 
125+ } ; 
126+ 
127+ /** 
128+  * Register a particular SimpleCommand or MacroCommand class as the handler for  
129+  * a particular Notification. 
130+  * 
131+  * If an command already been registered to handle Notifications with this name,  
132+  * it is no longer used, the new command is used instead. 
133+  * 
134+  * The Observer for the new command is only created if this the irst time a 
135+  * command has been regisered for this Notification name. 
136+  * 
137+  * @param  {string } notificationName 
138+  *  the name of the Notification 
139+  * @param  {Function } commandClassRef 
140+  *  a command constructor 
141+  * @return  {void } 
142+  */ 
143+ Controller . prototype . registerCommand =  function ( notificationName ,  commandClassRef ) 
144+ { 
145+     if ( this . commandMap [ notificationName ]  ==  null ) 
146+     { 
147+         this . view . registerObserver ( notificationName ,  new  Observer ( this . executeCommand ,  this ) ) ; 
148+     } 
149+ 
150+     this . commandMap [ notificationName ] =  commandClassRef ; 
151+ } ; 
152+ 
153+ /** 
154+  * Check if a command is registered for a given Notification 
155+  * 
156+  * @param  {string } notificationName 
157+  * @return  {boolean } 
158+  *  whether a Command is currently registered for the given notificationName. 
159+  */ 
160+ Controller . prototype . hasCommand =  function ( notificationName ) 
161+ { 
162+     return  this . commandMap [ notificationName ]  !=  null ; 
163+ } ; 
164+ 
165+ /** 
166+  * Remove a previously registered command to 
167+  * {@link  puremvc.Notification Notification} 
168+  * mapping. 
169+  * 
170+  * @param  {string } notificationName 
171+  *  the name of the Notification to remove the command mapping for 
172+  * @return  {void } 
173+  */ 
174+ Controller . prototype . removeCommand =  function ( notificationName ) 
175+ { 
176+     if ( this . hasCommand ( notificationName ) ) 
177+     { 
178+         this . view . removeObserver ( notificationName ,  this ) ; 
179+         this . commandMap [ notificationName ] =  null ; 
180+     } 
181+ } ; 
182+ 
183+ /** 
184+  * @static  
185+  * Remove a Controller instance. 
186+  * 
187+  * @param  {string } key  
188+  *  multitonKey of Controller instance to remove 
189+  * @return  {void } 
190+  */ 
191+ Controller . removeController =  function ( key ) 
192+ { 
193+     delete  this . instanceMap [ key ] ; 
194+ } ; 
195+ 
196+ /** 
197+  * Local reference to the Controller's View 
198+  *  
199+  * @protected  
200+  * @type  {puremvc.View } 
201+  */ 
202+ Controller . prototype . view =  null ; 
203+ 
204+ /** 
205+  * Note name to command constructor mappings 
206+  *  
207+  * @protected  
208+  * @type  {Object } 
209+  */ 
210+ Controller . prototype . commandMap =  null ; 
211+ 
212+ /** 
213+  * The Controller's multiton key 
214+  *  
215+  * @protected  
216+  * @type  {string } 
217+  */ 
218+ Controller . prototype . multitonKey =  null ; 
219+ 
220+ /** 
221+  * Multiton key to Controller instance mappings 
222+  *  
223+  * @static  
224+  * @protected  
225+  * @type  {Object } 
226+  */ 
227+ Controller . instanceMap =  [ ] ; 
228+ 
229+ /** 
230+  * @ignore  
231+  *  
232+  * Message constants 
233+  * @static  
234+  * @protected  
235+  * @type  {string } 
236+  */ 
237+ Controller . MULTITON_MSG =  "controller key for this Multiton key already constructed" 
0 commit comments