Question

I'm trying to create a namespace for my backbone app so I can make calls globally.

Normally, I'd just do it like this:

var myNamespace = window.myNamespace || {};
myNamespace.SomeView = Backbone.View.extend(..);

Not sure how to achieve this using require js

Était-ce utile?

La solution

You can do the same in your require or define calls, window's still there (if you work in browsers).

// views/app.js
define(['router', 'views/main-view'], function (router, mainView) {
  var App = function () { /* main app module */ };
  // do your "global" export
  window.App = App;
  return App;
});

// views/header-view.js
define(['views/app', 'models/user'], function (App, User) {
  // your header view code here
  // note that you have access to `App` already in a closure
  // but you can still talk to it by doing
  globalAppReference = window.App;
  globalAppReference === App; // true
});

The question is why would you need it? Ideally all your modules would be defined with requireJS, so you don't need to refer to them through global object.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top