Once I had identified and dealt with the definition versus loading problem, I had a short but productive streak, only to come to a complete halt because of another cryptic problem. While the definition versus loading problem is an obvious mistake (albeit easy to make, easy to overlook, and hard to diagnose), my next cryptic problem was not one that I, or the senior programmers that were kindly helping me, even knew to look for.
Symptoms
FYI
In both the Firefox and Chrome consoles, the following lines are generated by console.log
commands within my code, including the eventFactory –the very same eventFactory that clearly had a properly written getEventData method– …
|
|
Firefox
- My html showed up, but my controller variables weren’t being displayed.
- My Firefox console showed…
|
|
Canary Chrome
- My Chrome console showed…
|
|
Mistake
So, what was my mistake?
I had the following [1]…
|
|
instead of …
|
|
Note the order of $scope
and eventFactory
at the beginning of the array in comparison to their parameter order in the anonymous function.
Explanation
As I mentioned previously, the senior (relatively) programmers and I didn’t even suspect that this could cause a problem. At least we didn’t until I demonstrated that it did. But I had stumbled across the solution and no one in the group could explain why the mismatched order would cause a failure. We all knew that order doesn’t matter in arrays; only t clearly did in this context.
Thankfully, shortly after our cohort started, I was given a copy of “AngularJS Up & Running” by Shyam Seshadri & Brad Green. I’ve been busy enough with work and coursework that I didn’t even open it until a couple of weekends ago. At that point, I did a quick skim, looking specifically for information on $q and found it lacking in that area. I started skimming it in a little more depth yesterday, and wow; I can’t wait for a chance to go through it page by page! In the meantime, one of the tidbits I stumbled across explained why the above code failed, and it makes perfect sense.
First some more background is useful. Historically, if the modules being injected were not included, as strings, in the array with the anonymous function, then minification would functionally obfuscate the module name and the code wouldn’t work once minified. So the “Safe Style of Dependency Injection” should be used if the code is going to be minified. While there are steps toward rectifying the underlying problems, the safe style is still recommended. I strongly suggest checking out the textbox on pages 75-76 of “AngularJS Up & Running” for more information.
So why does the order of injection matter?
Well, the parameters that are used by the anonymous function are determined based on the order of the preceding items in the array. Hence, in
|
|
the first parameter eventFactory
is being assigned the '$scope'
module, while the $scope
parameter represents the 'eventFactory'
module. So, clearly things aren’t going to work as expected! Mystery solved.