-2

I am developing a application using Backbone.Marionette, I am loading the files using RequireJs, I decide to use the strict mode for my app, so I declared

"use strict" on the function. But I am getting error like this:

ught TypeError: Cannot set property 'App' of undefined 

when i remove the "use strict" from function it all works fine.

here is my function:

define(["jQuery", "underscore", "backbone", "marionette","routes"], 
    function($, _, Backbone, Marionette, Routes){

        "use strict";

        console.log(Marionette);

        this.App = new Marionette.Application();

        this.App.addInitializer(function(){
            var Route = new Routes();
            Backbone.history.start();
        });

        this.App.start();


}); 

What is wrong here? how to fix this issue or how to apply strict mode to my function?

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • It looks like `this` is `undefined` when RequireJS calls the function. Try using `window.App`, or simply even `App` to achieve what you are looking for. – Sharadh May 14 '14 at 09:39
  • I tried calling App but not works. it works by calling windows.App; But i prefer to call whether App or this.App - how can i get that? – 3gwebtrain May 14 '14 at 10:17
  • It would help to know your motive - Where do you want App to be accessible? – Sharadh May 14 '14 at 10:28
  • across the pages/files – 3gwebtrain May 14 '14 at 11:13
  • You can return `App` as result of the module. That way, wherever you `require (['AppFile'], function (App) { ... });` App is accessible. Else, `window.App` is the same as `App` - `window` is the default global namespace. Try `App = App {};` your js file, before the `define` line. – Sharadh May 14 '14 at 14:40

1 Answers1

0

If you add this to your main or config file you can then use the window as a dependency.

define('window',[],function(){return window;});
Nicholas
  • 768
  • 6
  • 16