1
+ ( function ( $ , undefined ) {
2
+
3
+ // Make sure the jQuery.mvc namespace exists.
4
+ $ . mvc = $ . mvc || { } ;
5
+
6
+ /**
7
+ * Version indicator for the jQuery.mvc library.
8
+ * @type String
9
+ */
10
+ $ . mvc . version = "1.0.0" ;
11
+
12
+ /**
13
+ * Creates a new jQuery.mvc module prototype with the given name. If the name includes
14
+ * a leading namespace (separated from the name by a dot), the prototype is stored at
15
+ * the location jQuery.mvc.<namespace>.<name> for further use.
16
+ * @param {String } name The name of the new module prototype, which may include a
17
+ * leading dot separated namespace.
18
+ * @param {Object } base The base module prototype from which to derive the new
19
+ * module prototype.
20
+ * @param {Object } proto The definition for the new module prototype.
21
+ * @return {Function } The constructor function for the new module prototype.
22
+ */
23
+ $ . mvc . module = function ( name , base , proto ) {
24
+ var fullname , namespace , baseObj , namespaceObj ;
25
+
26
+ // Resolve the module's name and namespace.
27
+ fullname = name . split ( "." ) ;
28
+ name = fullname [ 1 ] || fullname [ 0 ] ;
29
+ namespace = fullname [ 0 ] === name ? "" : fullname [ 0 ] ;
30
+ fullname = fullname . join ( "-" ) ;
31
+
32
+ // Create a new, uninitialized instance of the base MVC module and
33
+ // deep copy its options onto the instance so they aren't shared
34
+ // from the base prototype across multiple instances.
35
+ baseObj = new base ( ) ;
36
+ baseObj . options = $ . extend ( true , { } , baseObj . options ) ;
37
+
38
+ // Create the namespace for the new MVC module. This may be a temporary
39
+ // object when no namespace was specified.
40
+ namespaceObj = namespace
41
+ ? $ . mvc [ namespace ] = $ . mvc [ namespace ] || { }
42
+ : { } ;
43
+
44
+ // Create a new constructor function. This has to be a new unique function
45
+ // of which the prototype will be extended to create the new MVC module.
46
+ namespaceObj [ name ] = function ( ) {
47
+ // Allow instantiation without initializing for simple inheritance
48
+ if ( arguments . length ) {
49
+ this . _createModule . apply ( this , arguments ) ;
50
+ }
51
+ } ;
52
+
53
+ // Compose the complete prototype for the new MVC module.
54
+ namespaceObj [ name ] . prototype = $ . extend ( true , baseObj , {
55
+ name : name ,
56
+ namespace : namespace ,
57
+ base : base . prototype ,
58
+ constructor : namespaceObj [ name ]
59
+ } , proto ) ;
60
+
61
+ // Add or replace the internal __super method where necessary.
62
+ $ . each ( namespaceObj [ name ] . prototype , function ( name , fn ) {
63
+ if ( name === "constructor" ) { return true ; /* continue */ }
64
+
65
+ var fnSuper = base . prototype [ name ] ;
66
+ if ( fn !== fnSuper && $ . isFunction ( fn ) && $ . isFunction ( fnSuper ) ) {
67
+ fn . __super = fnSuper ;
68
+ }
69
+
70
+ } ) ;
71
+
72
+ return namespaceObj [ name ] ;
73
+ }
74
+
75
+
76
+ /**
77
+ * @constructor
78
+ * Creates a new jQuery.mvc module.
79
+ * @param {String } name The module's registered name.
80
+ * @param {Object } options A hash of options with which the module will
81
+ * be configured.
82
+ */
83
+ $ . mvc . Module = function ( name , options ) {
84
+
85
+ // Allow instantiation without initializing for simple inheritance
86
+ if ( arguments . length > 0 ) {
87
+ this . _createModule . apply ( this , arguments ) ;
88
+ }
89
+ }
90
+
91
+ $ . mvc . Module . prototype = {
92
+ /**
93
+ * The module's type name.
94
+ * @type String
95
+ */
96
+ name : "module" ,
97
+
98
+ /**
99
+ * A hash of option values configuring the module.
100
+ * @type Object
101
+ */
102
+ options : { } ,
103
+
104
+ /**
105
+ * Creates a new jQuery.mvc module.
106
+ * @param {String } name The module's registered name.
107
+ * @param {Object } options A hash of options with which the module will
108
+ * be configured.
109
+ */
110
+ _createModule : function ( name , options ) {
111
+ // The instance member reflects the instance name, while the prototype's
112
+ // member reflects the type name.
113
+ this . name = name ;
114
+ this . options = $ . extend ( true , { } , this . options , options ) ;
115
+ } ,
116
+
117
+ /**
118
+ * Calls the 'super' version of the calling method in the module's base module.
119
+ * <p>
120
+ * The pattern relies on the arguments.callee and arguments.callee.caller properties
121
+ * to store a reference on a function to its 'super' function and to obtain a reference
122
+ * to the calling function, respectively. The result is a virtual function call that
123
+ * will in all situations correctly handle the chain of inheritance -and- is safe for
124
+ * use with reentrant callback functions.
125
+ * </p>
126
+ * <p>
127
+ * Correctness comes at the cost of execution speed: the use of arguments.callee and
128
+ * arguments.callee.caller is expensive in terms of limiting compiler optimizations.
129
+ * It would be a bad idea to make many repeated calls to _super, e.g. , by using it
130
+ * inside tight loops. * </p>
131
+ * @param {* } ... A variable list of arguments to pass to the super method.
132
+ * @return {* } The return value of the super method call.
133
+ */
134
+ _super : function ( /* ... */ ) {
135
+ var caller = arguments . callee . caller ;
136
+
137
+ if ( caller && $ . isFunction ( caller . __super ) ) {
138
+ return caller . __super . apply ( this , arguments ) ;
139
+ } else {
140
+ throw "_super: No super-implementation available for this function."
141
+ }
142
+ } ,
143
+
144
+ /**
145
+ * Gets the name of the MVC module's registered instance.
146
+ * @returns {String } The instance's name.
147
+ */
148
+ getName : function ( ) { return this . name ; } ,
149
+
150
+ /**
151
+ * Broadcasts a notification to the jQuery.mvc manifold.
152
+ * @param {String } name The notification's name.
153
+ * @param {Object } body The notification's body or data.
154
+ */
155
+ _notify : function ( name , body ) {
156
+ var notification = $ . mvc . Notification ( name , body ) ;
157
+ $ . mvc . notifier . notify ( notification ) ;
158
+ }
159
+ }
160
+
161
+ } ) ( jQuery ) ;
0 commit comments