Shamoon
Shamoon

Reputation: 43639

How does RequireJS decide what to run and what to load?

I'm running an EmberJS app using RequireJS

In my routes.coffee file I'm defining my routing:

define ['jquery'
        'ember'
        '_'
        'cs!myapp/myapp'
        "cs!myapp/routes/index_route"
        ], ($, Ember, _, MyApp, IndexRoute) ->
    alert "Inside myapp route"
    MyApp.Router.map () ->
        @resource "play", path: '/play', () ->
            @resource "myapp", {}, () ->
                @resource "games", {}, () ->
                    @route "next"
                    @resource "frames", {}, () ->
                        @route "commercial"
                    @resource "trivia", {}, () ->
                        @route "commercial"
                @route "start"
                @route "howItWorks"
                @route "goodbye"
        @route "readyToStart"
        @route "noContent"

    MyApp.IndexRoute = IndexRoute

Specifically, as I understand it, that's a define not a require, so it shouldn't execute that code. But the alert goes off. So what is happening?

Upvotes: 1

Views: 29

Answers (1)

Louis
Louis

Reputation: 151511

It is true that the define you show in your answer won't immediately call the factory function you give to it. (The factory function is the callback you give to define.) However, the alert in routes.coffee will be called as soon as something requires routes.coffee either directly or indirectly. The routes.coffee module is required if it appears in the dependency list of a require call or of a define call.

Note that it is possible to require a module and not use it. RequireJS does not care whether the module is used or not. It only cares that a module is required. So a call like require(['foo']) will require the module foo and cause its factory function to be called even though it is not used in this require call.

Upvotes: 2

Related Questions