sazr
sazr

Reputation: 25928

Create a new Backbone view from within that view

How can I create a new Backbone view from within that view? For example; my view ModalDialog1 needs to (re)instantiate itself when ModalDialog2 is closed.

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 {

            }
        }
    })
});

Upvotes: 1

Views: 97

Answers (1)

Sushanth --
Sushanth --

Reputation: 55740

You could use the constructor of the view in question.

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

The second option would be is to have a method which initializes modalDialog1 when modalDialog2 closes.

Upvotes: 1

Related Questions