Search code examples
javascriptrequirejsmarionette

include javascript files in marionette project


I have a marionette project, and I have my main js file which looks like:

define(["marionette", "handlebars"], function(Marionette, Handlebars){
    ...
});

Now I want to make a structure of my big app. So I would like to separate the content of this function to different files. So how can I do something like:

define(["marionette", "handlebars"], function(Marionette, Handlebars){
    include routes.js
    include menu/model.js
    include menu/views.js
    ...
});

I think require.js can help me, but I don't know how. Any ideas?


Solution

  • To require a file in the AMD structure (like the one you describe), you can give the path to the file in the array and get the exported value as argument of the callback like this.

    define(
      [
        "marionette",
        "handlebars",
        "routes",
        "menu/model",
        "menu/views"
      ],
      function(Marionette, Handlebars, Routes, Model, MenuView){
        // ...
    });
    

    Since this is a very basic thing in AMD, you should really read the documentation of Require.JS (here) and explanation about AMD modules (like this one). You will find lot of information about the AMD structure, etc