0

I'm making a Marionette app for uploading files and I'm at the very beginning of it. To start things off, I will show you the files I'm working with:

upload_view.js

AppManager.module("PrimaryApp.Upload", function(Upload, AppManager, Backbone, Marionette, $, _){

    Upload.UploadPage = Marionette.ItemView.extend({
        template: "#upload-template"
    });
});

upload_controller.js

AppManager.module("PrimaryApp.Upload", function(Upload, AppManager, Backbone, Marionette, $, _){
Upload.Controller = {
    show: function(){

        var uploadView = new Upload.UploadPage();

        AppManager.regions.primary.show(uploadView);
    },
  };
});

app.js

var AppManager = new Marionette.Application();

AppManager.on("before:start", function() {
    var RegionContainer = Marionette.LayoutView.extend({
        el: "#app-container",

        regions: {
            primary: "#primary-region",
            secondary: "#secondary-region",
            header: "#header-region"
        }
    });

    AppManager.regions = new RegionContainer();
});

AppManager.on("start", function(){
    console.log(AppManager);
    AppManager.PrimaryApp.Upload.Controller.show();
})

AppManager.start();

When running this application in a browser I get this error:

Uncaught TypeError: Cannot read property 'Upload' of undefined   

For this line of code in app.js:

AppManager.PrimaryApp.Upload.Controller.show();

However, when I output AppManager to the console I get this:

enter image description here

Which shows that AppManager.PrimaryApp is indeed defined and contains all the submodules needed. Any ideas why my AppManager.PrimaryApp is undefined when called as a function, but then is defined when outputted to the console?

chadb768
  • 473
  • 1
  • 8
  • 16

1 Answers1

0

Figured out what was wrong!

AppManager.start();

Was in the wrong place and tried to run before AppManager.PrimaryApp was defined. However,

console.log(AppManager);

was somehow called after AppManager.PrimaryApp was defined resulting in a correct output. A simple move of my start() function fixed the order of things.

chadb768
  • 473
  • 1
  • 8
  • 16