Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've created code in each of my controllers and directives that calls an initer function in the main app.js module, the function just passes strings which are placed into an array and counted. Once all the controllers and directives have been accounted for, then the first function that runs in the main app is called.

I had to create this solution, because if I just call a function in my main app.js to another function in one of my other controllers, I may get undefined because it hasn't fully loaded yet.

I've built a dashboard app where I ran into this problem, and now building a mobile app using AngularJS and running into the same problem again.

This is a problem I have in development, where I want each module to be included in a separate script tag, makes it easier for development. I don't concatenate and minify until deployment. Thus each script module may load out of order, or slower than the app.js.

<!-- App modules -->
<script src="app/app.js"></script>
<script src="app/authentication/authFactory.js"></script>
<script src="app/api/apiFactory.js"></script>
// etc ...

The initer function in my main app.js file, it waits till the vm.pingArray has a length of 9 before it runs.

// Collects pings from Controllers and Directives
function initer(ping) {
    vm.pingArray.push(ping);
    // console.log('vm.pingArray.length = ', vm.pingArray.length);

    // Check if all modules have pinged in:
    if (vm.pingArray.length === 9) {

        if ($location.$$url !== "/main") {
            vm.customURL = true;
        } else {
            vm.tickerType = 'all';
        }

        // Init & start TickersPanel:
        if (vm.customURL) {
            InitTickersFactory.startTickersPanel(vm.tickerType, vm.selectedTicker);    
        } else {
            InitTickersFactory.startTickersPanel();
        }
    }
}

Code in my Directives Controllers, exactly the same, just the string names change:

// Store scope
ScopeFactory.saveScope('tagsPanel', vs);

// Ping root
root.main.initer('tagsPanelDirective');

^ as you can see, this allows for each Directive to fully load, store it's own scope into the ScopeFactory (so I don't get undefined later). Then it pings the initer function in the root app.js file.


This is the startTickersPanel function in the InitTickersFactory:

function startTickersPanel(type, ticker) {
    type   = type   || 'all';
    ticker = ticker || 'SPY';

    tickers = ScopeFactory.getScope('tickersPanel');
    tickers.tickersPanel.initTickersPanel(type, ticker, true);
}

If I don't have my pingArray check code in the app.js module, and just call InitTickersFactory.startTickersPanel(); in app.js, I will get tickers.tickersPanel is undefined inside of the initTickersFactory.js.

share|improve this question
    
uhm.... angular is not meant to be used this way. angular automatically bootstraps after the DOM is ready, so as long as you load your controllers/etc using script tags (or better yet, have it concatenated via build process) there's no need for this. What exactly are you doing here? Is your source on Github? You can use angular.bootstrap(domElement, arrayOfModules) to bootstrap angular manually if you don't want to use ng-app –  A Red Herring Sep 1 at 18:12
    
Sorry yeah I meant to put in there, that this problem happens when I include each module as a separate script tag, I'm still in development. I only use the concatenated minified file before deployment. Easier to find out what line in what module a bug has occurred in. Otherwise, I'd have to check what line it is inside my concatenated app.js then scroll around to figure out what module, then open up that module. Instead of just seeing what line it's in and the exact module in the inspector –  Leon Gaban Sep 1 at 18:15
    
This is likely because you're not waiting until the DOM has loaded before you do work on Angular, or because you are relying on implicit ordering of script tags. This is bad. Don't do this. Use a solution like RequireJS, Browserify or Webpack to aid your dependency management. –  A Red Herring Sep 1 at 18:18
    
Interesting, I've known about RequireJS, never knew what it's benefits where. Just got done with a 10 min video so have a much better idea now. I use Gulp for my build making, but I get that RequireJS would be great for loading up all my dev modules. I wonder though about having to use define([ in every file/lib I load, even having to place it in 3rd party stuff like AngularJS and other plugins. –  Leon Gaban Sep 1 at 18:46
1  
I personally prefer Webpack which uses var angular = require('angular'); syntax. Trust me, it wil lmake your life a lot easier down the road.. and your team's life easier too –  A Red Herring Sep 1 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.