不能访问Requirejs模块中的另一个类
Cannot access to another class inside Requirejs module
我无法访问我的"App"对象,尽管我需要它在我的模块内与已定义的语句。你能解释一下为什么我的代码不能工作吗?
这是我的main.js文件:require(['underscore', 'backbone', 'App'], function(_, Backbone, App) {
window.App = new App();
Backbone.history.start();
return window.App.init();
});
这是我的app.js文件:
define(['underscore', 'backbone', 'Views/ApplicationView'], function(_, Backbone, ApplicationView) {
'use strict';
var App;
return App = (function() {
function App() {
this.init = __bind(this.init, this);
}
App.prototype.init = function() {
this.view = new ApplicationView();
};
return App;
})();
});
最后是applicationview。js
define(['underscore', 'backbone', 'views/AbstractView', 'App'], function(_, Backbone, AbstractView, App) {
'use strict';
var RegisterView;
return RegisterView = (function(_super) {
__extends(RegisterView, _super);
function RegisterView() {
this.init = __bind(this.init, this);
return RegisterView.__super__.constructor.apply(this, arguments);
}
RegisterView.prototype.init = function() {
RegisterView.__super__.init.apply(this, arguments);
this.model = App.user; // APP IS UNDEFINED
};
})(AbstractView);
});
我在这段代码中看到的唯一问题是循环依赖。通常,您希望设计代码以避免它们。如果不能,则应该修改上面的代码,使分配this.model
(在RegisterView.prototype.init
中)的行如下所示:
this.model = require("App").user;
必须这样做,因为模块的工厂函数(给予define
的匿名函数)将获得的App
必然是undefined
,因为到RequireJS加载此模块时,App
的模块尚未完成加载。像上面那样执行require
调用会导致RequireJS稍后在App
模块加载完成后获取该值。
记录。
相关文章:
- 从一个模块暴露到另一个模块
- 如何从另一个模块调用函数
- 无法调用requirejs中的另一个模块
- 在另一个模块中重用私有jQuery函数
- 从另一个模块访问作用域的指令
- nodejs 将值从一个模块传递到另一个模块
- JavaScript 将一个模块“导入”到另一个模块中
- 如何将方法导出到另一个模块
- 使用 on nodejs 从另一个模块捕获事件
- 来自另一个模块的 Angularjs 路由控制器
- 来自另一个模块的 Angular JS Service DI
- 访问angularjs中另一个模块中定义的工厂
- 无法访问另一个模块's函数
- 使用Jasmine测试调用另一个模块的模块
- 如何将app=express()传递到另一个模块中
- 如何在angular js中将服务从一个模块调用到另一个模块服务
- 如何从模块依赖关系(另一个模块)调用函数
- 在node.js中用一个模块替换另一个模块
- 我可以向另一个模块中用作依赖项的requirejs模块添加一个方法,并在每次将该模块作为依赖项加载时使其可用吗
- 调用AngularJS中另一个模块中控制器中的函数