Reputation: 730
My understanding of RequireJS was that if you required module B from module A, and then module B required module C, module C would be able to access module A. Is this correct?
If not, how would I get module C to use A?
Here's my problem:
I have a Backbone model Accounts, a coffee file called Core, and the cmsmain file used by the require script tag in my HTML.
cmsmain.coffee
requirejs.config
baseUrl: 'assets/javascripts/cms'
paths:
jquery: '../lib/jquery'
backbone: '../lib/backbone'
underscore: '../lib/underscore'
AccountModel: 'model/account'
require ['core', 'jquery', 'underscore', 'backbone'], (Core, $, _, Backbone) ->
Core.Start()
core.coffee:
define () ->
# Declare the app namespace
Core = window.Core =
Classes:
Models: {}
Collections: {}
Controllers: {}
Views: {}
Rendered:
Models: {}
Collections: {}
Controllers: {}
Views: {}
Templates: {}
Routes: {}
Start: ->
alert("Started")
return Core
model/account.coffee
# Account Model
define () ->
class Core.Classes.Models.Account extends Backbone.Model
initialize: ->
login: (username, password) ->
Everything worked fine until i required the Account model, then I got:
Uncaught ReferenceError: Core is not defined
Even after I required Core in the accounts model i got:
Uncaught TypeError: Cannot read property 'Classes' of undefined
Both errors were generated by the account model
Upvotes: 1
Views: 83
Reputation: 25994
You need to use the value returned by the RequireJS dependency (CoffeeScript approximative...):
define ['core'], (Core) ->
class Core.Classes.Models.Account extends Backbone.Model
initialize: ->
login: (username, password) ->
Otherwise, RequireJS won't wait for Core
to be loaded before trying to access the Classes
attribute.
See for example https://github.com/davidsulc/structuring-backbone-with-requirejs-and-marionette/blob/master/assets/js/apps/contacts/edit/edit_controller.js
Upvotes: 1