从该视图中创建一个新的Backbone视图

时间:2017-04-12 23:39:08

标签: javascript backbone.js

如何在该视图中创建新的Backbone视图?例如;当ModalDialog1关闭时,我的观点ModalDialog2需要(重新)实例化。

define('ModalDialog1.View',
    [
        'modal_dialog1.tpl'
    ,   'ModalDialog2.View'
    ,   'Backbone'
    ,   'underscore'
    ],
    function(
        modal_dialog1_tpl
    ,   ModalDialog2View
    ,   Backbone
    ,   _
    )
{
    'use strict';

    return Backbone.View.extend({
        template: modal_dialog1_tpl

    ,   events: {
            'click a[data-modal-id="why-need-info"]': 'openModalDialog2'
        }

    ,   openModalDialog2: function() {

            var self = this;

            var closeCallback = function() {
                // How to reinstantiate this view/self??
                var modalDialog1 = new self();

                modalDialog1 .showInModal();  
            }

            var view = new ModalDialog2View({closeCallback: closeCallback})
                .showInModal();
                // On calling showInModal the current modal view (this) is destroyed
        }

    ,   getContext: function()
        {
            return {

            }
        }
    })
});

1 个答案:

答案 0 :(得分:1)

您可以使用相关视图的构造函数。

var modalDialog1 = new self.constructor();
modalDialog1.showInModal();

第二个选项是在modalDialog1关闭时有一个初始化modalDialog2的方法。

相关问题