Defining a global application namespace with Require.js and Backbone

I am new to Require.js and I am trying to do something that I thought would be simple but starting to get pain.

I am trying to define a global namespace for my Backbone application and load it as a module. Here is my namespace (main.js):

define(
['jquery',
  'underscore',
  'backbone',
  'GlobalRouter'
],
function($, _, Backbone) {
var App= {
    Models: {},
    Views: {},
    Collections: {},
    Routers: {},
    init: function() {
        new App.Routers.GlobalRouter();
        Backbone.history.start();
    }
}
return App;

});

and here is my config.js file:

require.config({
// your configuration key/values here
baseUrl: "js", // generally the same directory as the script used in a data-main attribute for the top level script
paths: {
    'jquery' : '//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min',
    'underscore': 'vendor/underscore-min',
    'backbone': 'vendor/backbone-min',
    'marionette': 'vendor/backbone.marionette',
    'main' : 'main'
}, // set up custom paths to libraries, or paths to RequireJS plugins
shim: {
    'underscore': {
        exports: '_'
    },

    'backbone': {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
    },

    'main' : {
        deps: ['underscore', 'jquery', 'backbone', 'GlobalRouter'],
        exports: 'TEWC'
    }

} // used for setting up all Shims (see below for more detail)
});

define([
'jquery',
   'underscore',
  'backbone',
  'main'
],
function($, _, Backbone, App, GlobalRouter) {
console.log(App)
alert('hit ')
  $(function() {
       App.init();
    });
 }

);

and for good measure, here is my router:

define([
'jquery',
'underscore',
'backbone',
'main'
],
function($, _, Backbone, TEWC) {

TEWC.Routers.GlobalRouter = Backbone.Router.extend({
    routes: {
        "" : "index",
        "documents/:id" : "edit",
        "login" : "login"
    },

    edit: function(id) {
        alert('edit')
    },

    index: function() {
        alert('index')
    },

    login: function() {
        alert('login')
    }
});

});

In the past, I have received the message "undefined". Now after a couple of minutes I get a download timeout error:

Uncaught Error: Load timeout for modules: main

However, the warning does not start, and main.js does not seem to load, but I believe that the router does and it does not bark that TEWC is undefined - so it can load even though it is not on my Network tab?

This is probably a newbie question - does anyone know about this?

+5
2

GlobalRouter, callback

define([
'jquery',
   'underscore',
  'backbone',
  'main'
],
function($, _, Backbone, App, GlobalRouter) {
console.log(App)
alert('hit ')
  $(function() {
       App.init();
    });
 }

GlobalRouter define

-, main.. , URL- ? , .

+2

, , config.js, require.config(), define() require().

. :

define([
'jquery',
   'underscore',
  'backbone',
  'main'
...

define require, , ; .

, , GlobalRouter , .

+2
source

All Articles