Search code examples
extjsextjs6extjs6-classic

How to get the data from the window to the main screen


On the success of some Ajax call I am getting opening a window and the response I attach with the window. Here is my code.

success: function (response) {
    var data = Ext.decode(response.data);
    var window = Ext.widget('win');
    window.data = data;
    window.show();
},

Now I am doing some manipulation and all the manuplitaed data are available in window object. After saving I am closing the window. On Parent when I am trying to acesss like this I am not getting any value. How to deal with this.

someFunction : function(){
    var window = Ext.widget('win');
        window.data ; // Fail (No data available).
}

Solution

  • Window object in success() and in someFunction() are not same as Ext.widget will create a separate instance in both the functions. so data wont be persisted. To persist the data you can either use a config or viewModel.

    I have created a sample code as per your scenarios to persist the data on window close using configs which acts kind of global to that specific view and its controller.

    Ext.application({
        name : 'Fiddle',
    
        launch : function() {
            Ext.create("Ext.form.Panel",{
                renderTo: Ext.getBody(),
                config: {
                    win: Ext.widget('window'), //if we store window, on close we loose it
                    data: "" //it will persist, so this can be used
                },
                items: [
                    {
                        xtype: 'button',
                        text: 'call',
                        handler: function(){
                            var me = this;
                            Ext.Ajax.request({
                             url: 'data1.json',
    
                                success: function(response, opts) {
                                    var data = Ext.decode(response.responseText);
                                    var window = me.up().config.win;
                                    me.up().config.data = data;
                                    window.data = data;
                                    window.show();
                                },
    
                                failure: function(response, opts) {
                                  console.log('server-side failure with status code ' + response.status);
                                }
                            });
                        }
                    }, {
                        xtype: 'button',
                        text: 'Get Value',
                        handler: function(){
                           console.log(this.up().config.win.data);
                           console.log(this.up().config.data);
                        }
                    }
                ]
            });
        }
    });
    

    Here the handler for first button will represent your ajax calls success and handler for second button represents your someFunction.

    Here i kept two configs, win and data. win is a window object which will be destroyed on close so we cant use it. second is data which is a normal variable, which will be persisted unless respective view will be destroyed. so you can use 2nd config.

    You can find working fiddle here.