@@ -82,45 +82,43 @@ classFns =
82
82
# Add the `deps` to the controller's $inject annotations.
83
83
parent .$inject = deps
84
84
85
- # compileController: (el, controllerName) ->
86
- # el.setAttribute('data-ng-controller', controllerName);
87
- # window.setTimeout ->
88
- # angular.element(document).injector().invoke ($compile) ->
89
- # scope = angular.element(el).scope()
90
- # $compile(el)(scope)
91
- # , 0
92
-
93
- registerSelector : (appInstance , selectorString , parent ) ->
85
+ registerSelector : (appInstance , selector , parent ) ->
94
86
@selectorControllerCount ++
95
- controllerName =
96
- " #{ selectorString .replace (/ \W / g , ' ' )} ClassySelector#{ @selectorControllerCount } Controller"
87
+ controllerName = " ClassySelector#{ @selectorControllerCount } Controller"
97
88
appInstance .controller controllerName, parent
98
89
99
- els = window .jQuery ? (selectorString) or document .querySelectorAll (selectorString)
100
- el .setAttribute (' data-ng-controller' , controllerName) for el in els
90
+ if angular .isElement (selector)
91
+ selector .setAttribute (' data-ng-controller' , controllerName)
92
+ return
101
93
102
- register : (appInstance , name , deps , parent ) ->
103
- if name .indexOf (' $' ) is 0
104
- # If first character of `name` is '$' then treat it as a selector
105
- @ registerSelector (appInstance, name .slice (1 ), parent)
106
- else
107
- # Register the controller
108
- appInstance .controller name, parent
94
+ if angular .isString (selector)
95
+ # Query the dom using jQuery if available, otherwise fallback to qSA
96
+ els = window .jQuery ? (selector) or document .querySelectorAll (selector)
97
+ else if angular .isArray (selector)
98
+ els = selector
99
+ else return
100
+
101
+ for el in els
102
+ if angular .isElement (el)
103
+ el .setAttribute (' data-ng-controller' , controllerName)
104
+
105
+ create : (appInstance , classObj , parent ) ->
106
+ if classObj .el || classObj .selector
107
+ # Register the controller using selector
108
+ @ registerSelector (appInstance, classObj .el || classObj .selector , parent)
109
+
110
+ if angular .isString (classObj .name )
111
+ # Register the controller using name
112
+ appInstance .controller classObj .name , parent
113
+
114
+ deps = classObj .inject
109
115
110
116
# Inject the `deps` if it's passed in as an array
111
117
if angular .isArray (deps) then @ inject (parent, deps)
112
118
113
119
# If `deps` is object: Wrap object in array and then inject
114
120
else if angular .isObject (deps) then @ inject (parent, [deps])
115
121
116
- create : (module , name , deps , proto , parent ) ->
117
- # Helper function that allows us to use an object literal instead of coffeescript classes (or prototype messiness)
118
- c = class extends parent
119
- @ register (name, deps, module )
120
- for own key,value of proto
121
- c :: [key] = value
122
- return c
123
-
124
122
125
123
origMethod = angular .module
126
124
angular .module = (name , reqs , configFn ) ->
@@ -133,30 +131,26 @@ angular.module = (name, reqs, configFn) ->
133
131
134
132
# If this module has required 'classy' then we're going to add `classyController`
135
133
if reqs and ' classy' in reqs
136
- class classyController
137
- # `classyController` is only a set of proxy functions for `classFns`,
138
- # this is because I suspect that performance is better this way.
139
- # TODO: Test performance to see if this is the best way to do it.
134
+ module .cC = module .classyController = (classObj ) ->
135
+ c = class classyController
136
+ # `classyController` is only a set of proxy functions for `classFns`,
137
+ # this is because I suspect that performance is better this way.
138
+ # TODO: Test performance to see if this is the most performant way to do it.
140
139
141
- __classyControllerScopeName : ' $scope'
140
+ __classyControllerScopeName : ' $scope'
142
141
143
- @ register: (name , deps ) ->
144
- # Registers controller and optionally inject dependencies
145
- classFns .register (module , name, deps, @ )
142
+ # Create the Classy Controller
143
+ classFns .create (module , classObj, @ )
146
144
147
- @ inject : ( deps ... ) ->
148
- # Injects the `dep`s
149
- classFns .inject (@ , deps )
145
+ constructor : ->
146
+ # Where the magic happens
147
+ classFns .construct (@ , arguments )
150
148
151
- @ create: (name , deps , proto ) ->
152
- # This method allows for nicer syntax for those not using CoffeeScript
153
- classFns .create (module , name, deps, proto, @ )
149
+ for own key,value of classObj
150
+ c :: [key] = value
154
151
155
- constructor : ->
156
- # Where the magic happens
157
- classFns .construct (@ , arguments )
152
+ return c
158
153
159
- module .cC = module .classyController = classyController
160
154
161
155
return module
162
156
0 commit comments