简体   繁体   English

在Backbone.JS中使用嵌套视图

[英]Working with nested views in Backbone.JS

I'm developing an app with Backbone.JS, consisting of a main view (IndexView) with a menu, an HTML5 video loop and a div for content (#container). 我正在使用Backbone.JS开发一个应用程序,该应用程序包括一个带有菜单的主视图(IndexView),一个HTML5视频循环和一个用于内容的div(#container)。 The idea is that when the app is initialized, depending on the route, a view will be render and displayed on the #container element. 想法是,在初始化应用程序时,根据路线,将在#container元素上呈现并显示视图。 Regardless of the route, the IndexView should always be shown. 无论采用哪种路线,都应始终显示IndexView。 This is what I have: 这就是我所拥有的:

router.js: router.js:

var initialize = function () {
    // The IndexView will be rendered when the app is initialized
    console.log("Rendering index view...");
    var indexView = new IndexView();
    indexView.render();

    var app_router = new AppRouter;

    // One of the routes
    app_router.on('route:about', function () {
        var aboutView = new AboutView();
        aboutView.render(); 
    });

    // Other routes here…
    Backbone.history.start();
};

return {
    initialize: initialize
};

views/index.js: views / index.js:

define([
    'jquery',
    'underscore',
    'backbone',
    'text!templates/index.html'
], function ($, _, Backbone, indexTemplate) {
    var IndexView = Backbone.View.extend({
        el : $("body"),
        render : function () {
            var data = {};
            var compiledTemplate = _.template(indexTemplate, data);
            this.$el.html(compiledTemplate);
        }
    }); 

    return IndexView;
});

views/about.js: 视图/about.js:

define([
    'jquery',
    'underscore',
    'backbone',
    'text!templates/about.html'
], function ($, _, Backbone, aboutTemplate) {
    var AboutView = Backbone.View.extend({
        el : $("#container"),
        render : function () {
            var data = {};
            var compiledTemplate = _.template(aboutTemplate, data);
            this.$el.html(compiledTemplate);
        }
    }); 

    return AboutView;
});

Well, the problem is that the IndexView is rendered properly, but the other views are not. 好吧,问题在于IndexView可以正确呈现,而其他视图则不能。 I suspect it is because, for some reason, they don't see the #container element created by the IndexView. 我怀疑这是因为由于某种原因,他们没有看到IndexView创建的#container元素。 I say this because if I have those views rendering to the body element instead, they do it just fine. 我之所以这样说是因为,如果我改为将那些视图呈现给body元素,那么它们就做得很好。

Any suggestions? 有什么建议么? Thanks in advance! 提前致谢!

Your problem is that the statement assigning the el is evaluated to early (that is it is being evaluated when your view is being defined as opposed to when you create your actual view which is before your index view has been rendered. What you should do is either pass in the el when you instantiate the view or you can manually assign it in the initialize method of your view. 您的问题是分配给el的语句评估为早期(即在定义视图时进行评估,而不是在创建实际视图之前(在呈现索引视图之前)进行评估。您应该做的是可以在实例化视图时传递el ,也可以在视图的initialize方法中手动分配它。

For example to pass in the el 例如传递el

var myAboutView = new AboutView({el: $('#container')};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM