我正在尝试为我的主干应用程序创建一个命名空间,以便我可以在全局范围内进行调用。
通常情况下,我会这样做:
var myNamespace = window.myNamespace || {};
myNamespace.SomeView = Backbone.View.extend(..);
不知道如何使用 require js 来实现这一点
最佳答案
您可以在 require
或 define
调用中执行相同的操作,window
仍然存在(如果您在浏览器中工作)。
// 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
});
问题是你为什么需要它?理想情况下,您的所有模块都将使用 requireJS 定义,因此您不需要通过全局对象引用它们。
关于javascript - 使用 require js 为 Backbone 应用程序命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13705953/