我正在加载一个包含主干视图的 requireJS 模块。在该视图内部,在“初始化”期间,还加载了包含在单独模块中的子视图。完成后,我想为新加载的子视图添加一个监听器。但是,此操作失败,因为应该加载的子模块由于某种原因尚未注册。在下面的代码中,当我记录“that.childModule”时,它返回一个未定义的值,如果模块加载正确,则不应出现这种情况。可能是什么问题呢
//Sample of a requireJS module that contains a backbone view, which in turn loads other subviews
define(function (require) {
//Narrate dependencies
'use strict';
var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
//Create the parentView
var ParentView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.render();
//Make the subViews
var that = this;
require([
'models/common/childModel',
'views/common/childView'
], function(ChildModel, ChildView){
that.ChildModule = new ChildView({
el: $(".theDiv", that.el),
model: new ChildModel()
});
});
console.log(that.ChildModule)
this.listenTo(this.ChildModule.model, 'change:focus', this.blur);
},
render: function() {
$(this.el).addClass('something');
$(this.el).append('<div class="theDiv"></div>');;
return this;
},
});
return ParentView;
});