0

I am using requireJS in my sample demo app. This is my code:

require({
    paths: {
        ToDoModel: '/Scripts/ToDoModel.min',
        ToDo: '/Scripts/ToDo.min',
        ToDoService: '/Scripts/ToDoService.min'
    }
}, ['ToDo'], function (ToDo) {

    ko.applyBindings(new ToDo(), $('#toDo')[0]);
}
);

I know I need some of the files as soon the app loads. Thus I want to combine ToDoModel, ToDo and ToDoService all into one file and yet define them as separate like this:

define('ToDoModel', [], function () {
    ToDoModel = function (data) {

        ko.mapping.fromJS(data, {}, this);


    }
    return ToDoModel;
});

All the moment all are in separate files as you can see in the paths attribute.

Jack
  • 7,433
  • 22
  • 63
  • 107

1 Answers1

2

RequireJS does not require that define calls be in different files. It's just that in development "one file, one module (i.e. one define)" works pretty well. In fact, the RequireJS optimizer (r.js) combines multiple define calls into one file during optimization. No problem there.

The setup you show in your question looks to me like a development setup. Normally, in development we have RequireJS load modules as separate files, not minified. (Your example suggest that you are loading midified code.) For deployment, we use r.js to minify and concatenate the various modules into one file that RequireJS loads.

Based on what you show in your question, if you did not use minified files you'd be able to use a configuration like this:

{
    baseUrl: "/Scripts"
}

and RequireJS would be able to find your individual modules on the basis of their names because they would not have .min in their names.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • But if we have to concatenate every module into a single file and download whole file irrespective of other modules are required or not then what purpose does requireJS serve? I thought the whole purpose of RequireJS was to download JS / text template files automatically as and when need arises? – Jack Mar 06 '14 at 12:38
  • Look at [this question](http://stackoverflow.com/questions/20515679/requirejs-to-bundle-or-not-to-bundle/20518314#20518314), and [this one](http://stackoverflow.com/questions/21272578/when-using-require-js-and-creating-a-single-file-using-r-js-do-we-still-get-the/21307947#21307947). – Louis Mar 06 '14 at 12:44