我将 Require.js 与 Backbone.js 和 Underscore.js 一起使用,并且我有一个嵌套 View ,当作为依赖项调用时,它显示为未定义,但是当我在同一个模块中有两个 View 时,它们工作正常。我想知道我做错了什么。这是一个例子:
subview .js
define([
'jQuery',
'Underscore',
'Backbone',
], function ($, _, Backbone) {
var ChildView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
this.render();
},
});
return ChildView;
});
父 View .js
define([
'jQuery',
'Underscore',
'Backbone',
'src/views/child-view'
], function ($, _, Backbone, ChildView){
var ParentView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
this.render();
},
render: function () {
child = new ChildView({});
}
});
return ParentView;
});
我在尝试调用新的 ChildView 时收到“Uncaught TypeError: undefined is not a function”。如果我在 Parentview 外部但在 parentview.js 内部引用 ChildView,它会显示 View ,但作为一个对象。
最佳答案
单从你的代码来看,应该没有问题,我测试了你的代码居然没有发现问题。 这是我的测试代码,你可以试试: http://files.cnblogs.com/justinw/test_byfejustin.zip
我觉得可能是你的require.js有问题,你可以把我的代码包里的test_byfejustin\js\libs\require\require.js换成你的require.js再试试.
关于javascript - 嵌套依赖性 Backbone.js View 与 Require.js Backbone.js 导致 View 加载为对象而不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264525/