I've started using the Revealing Module Pattern a lot more than I ever have before. While many things have contributed to this shift, one feature, or lack thereof, was the inability to use Base Controllers in AngularJS. Since Controllers cannot be generated within Factories, it seemed that Base Controllers could only be provided through global references (which goes against everything that AngularJS stands for). That said, after my conversation yesterday, I wanted to give Base Controllers one more try.
NOTE: This is just an experiment in using Factories to generate Controllers. I am not necessarily advocating this approach - see final paragraph.
In JavaScript, when you invoke a constructor function, the return value of the construtor matters. If you don't return anything, JavaScript will use the instantiated object as the result of the invocation. If, however, you return a value (other than "this") from the constructor function, JavaScript will use that return value as the result of the invocation.
When it comes to AngularJS, we can leverage this language feature to fake the ability to create Controllers inside of Factories. The idea is simple, but a bit confusing. And, after some trial and error, I finally got it to work. What we're going to do is turn our Controller into a factory.
Your new post is loading...