3

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).
}
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
David
  • 4,266
  • 8
  • 34
  • 69
  • 1
    You need to think a bit more asynchronously. window.data gets executed immediately after the window is called, i.e. before the data has changed. Consider adding a callback function or some sort of handler so you know when the window has been closed and data can be added to the variable – DGK Sep 13 '19 at 13:28
  • Thanks for the idea @Laurens any link the way I can use callback. – David Sep 13 '19 at 13:35
  • i guess default closeAction of the window is destroy, set it to hide and it should work - docs here: https://docs.sencha.com/extjs/6.5.3/classic/Ext.panel.Panel.html#cfg-closeAction – hwsw Sep 14 '19 at 21:43
  • @David share a fiddle reproducing the mentioned behavior? – Jaydeep Sep 16 '19 at 07:54
  • @JD I am trying to create the fiddler but hard to create that. Do you have any idea how we can get the value from the window to the main screen, once window is close. – David Sep 16 '19 at 08:11
  • there are couple of ways to persist the data, wanted to see the code to tell you the way you can use.. what issue are you facing in creating fiddle by the way? – Jaydeep Sep 16 '19 at 08:30
  • 1
    I will try to create a fiddle, current I am attaching data to window object. Let me create a fiddle. Because not sure how to use call back and all. Thanks – David Sep 16 '19 at 08:37

1 Answers1

1

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.

Jaydeep
  • 1,686
  • 1
  • 16
  • 29
  • Actually I have done the same thing. Lets wait for few more hour, In case no one show up I will accept this. – David Sep 23 '19 at 04:32