Search code examples
backbone.jsrequirejs

Why requireJs wouldn't load defined dependency modules?


I am having problem while using Require.js to load dependencies for my module. Basically I have following module in which I define extension of Backbone Model.

define(["models/services/ProjectServices"],

    function (ProjectServices) {

        var SomeModel = Backbone.Model.extend({

             sample: function () {
             var servicesFromDependency = ProjectServices; //undefined
             var projectServices = window.require.s.contexts._.defined["models/services/ProjectServices"]; //defined and Ok
             }
         });
        return SomeModel;
    }
);

In this module I want to use already defined ProjectServices module. In order to do that I add it as a dependency. The thing is that within defined sample function ProjectServices is showing as undefined. But if I look directly into require defined modules it is showing there correctly and I can use it (although I do not want as I don't like to hack it this way). To add more context, I am also using this ProjectServices dependency on other module and there it is loaded properly through define function.

Any suggestions on why module would not be loaded?


Solution

  • Try this inside a module:

    var ProjectServices = require('models/services/ProjectServices');
    

    I think in many situations, there is no need for window global assignment and I try to avoid while using requirjs.