简体   繁体   English

混淆骨干.js中的this.el和子视图

[英]Confusing this.el in backbone.js with subviews

I've a big problem with subviews in backbone.js. 我对骨干.js中的子视图有很大的疑问。 I've two views and I want to render the second one into an element of the first one. 我有两个视图,我想将第二个视图渲染为第一个视图的元素。 So i defined this.el in the second view which is referenced to a section (section#user-data) in the first view. 所以我在第二个视图中定义了this.el ,该视图在第一个视图中引用了一个节(section#user-data)。

I create an object of the second view after first view has been rendered and if i write alert($('#user-data').length) before I create my object I get a 1 so it exists. 在渲染第一个视图之后创建了第二个视图的对象,并且如果在创建对象之前写了alert($('#user-data').length) ,我将得到1,因此它存在。

But inside the second view I get an undefined. 但是在第二个视图中,我得到了一个不确定的信息。 If I log this.el in any method. 如果我以任何方法登录this.el But if I use eg body for this.el everything works as expected. 但是,如果我在this.el使用例如body,一切都会按预期进行。

What's the reason for that? 是什么原因呢?

Example: 例:

   /**
     * The example of a view for profile's component
     *
     *
     * @module Profile
     * @submodule Frontend
     * @class ProfileView
     * @constructor
     *
     */
    ProfileView = Backbone.View.extend({

        // The tagname wrapping the rendered content
        // The template for the current view
        template : _.template('COMPONENTS_VIEW_FRAME'),

        /**
         * The initialisation for the current view (kind of constructor)
         *
         * @method initialize
         */
        initialize : function() {
        },

        /**
         * The rendering process for the current view
         *
         * @method render
         * @param {String} oLocas the data to render inside this view
         */
        render : function(oLocas) {

            $(this.el).html(this.template(oLocas));

            return this;
        }
    });

           /**
     * The example of a view for profile's component
     *
     *
     * @module Profile
     * @submodule Frontend
     * @class ProfileView
     * @constructor
     *
     */
    ProfileSectionView = Backbone.View.extend({

        // The tagname wrapping the rendered content

        // The template for the current view
        template : _.template('COMPONENTS_VIEW_OPTION_SECTION'),

        el : $('#user-data'),

        events : {
            'click a.open_close_section' : 'doIt'
        },

        headline : 'ddd',

        /**
         * The initialisation for the current view (kind of constructor)
         *
         * @method initialize
         */
        initialize : function() {
            alert($(this.el).length);
        },

        doIt : function(event) {
            alert('jo');
        },

/**
         * The rendering process for the current view
         *
         * @method render
         * @param {String} eventName the name of the triggered event
         */
        render : function(oLocas) {

            $(this.el).html(this.template(oLocas));

            return this;
        }
    });
            // Here I start to use the first view
            var oProfileView = new ProfileView();

                $('#profile').html(oProfileView.render().el).fadeIn(500,    function() {

                    $(this).removeClass('closed');
                                            // Here I create the second view
                    var oProfileSectionView = new ProfileSectionView({
                        model : this.model
                    });

                    oProfileSectionView.render({
                        headline : 'XXX',
                        sectionrows : 'ddd'
                    }).el;

                });

This is a little gotcha with JavaScript. 这是JavaScript的小技巧。

JavaScript is looking for that element when you're defining ProfileSelectionView . 当您定义 ProfileSelectionView时,JavaScript正在寻找该元素。 So this: 所以这:

ProfileSectionView = Backbone.View.extend({
    // ...
    el : $('#user-data'),
    // ...
});

Is actually being evaluated right there in the definition of ProfileSelectionView , not later on when you instantiate an instance of ProfileSelectionView . 实际上是在ProfileSelectionView的定义中进行评估,而不是稍后实例化ProfileSelectionView的实例时进行评估。

To fix this, move the defining of el into the initialization: 要解决此问题,请将el的定义移至初始化中:

ProfileSectionView = Backbone.View.extend({
    // ...
    // Do not define the el here. I've commented it out
    // el : $('#user-data'),
    // ...
    initialize : function() {
        this.setElement($('#user-data'));
    },
    // ...
});

Or even better, you can just pass the element in when creating the ProfileSelectionView (as noted in the Backbone.js docs ): 甚至更好的是,您可以在创建ProfileSelectionView时将元素传入(如Backbone.js docs中所述 ):

var oProfileView = new ProfileView();

$('#profile').html(oProfileView.render().el).fadeIn(500,    function() {
    // ...
    var oProfileSectionView = new ProfileSectionView({
        model : this.model,
        el: $('#user-data')
    });
    // ...
});

By doing it this way, you don't have to define an initialize function in ProfileSelectionView . 通过这种方式,您不必在ProfileSelectionView定义一个initialize函数。

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

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