使用requireJS从父视图中加载backbone.js子视图

时间:2013-08-25 19:27:47

标签: jquery backbone.js requirejs synchronous

我正在加载包含骨干视图的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;
});

0 个答案:

没有答案