简体   繁体   中英

How to call function only after the executing of the other function is completly finished

In the following code, I would like to reload the siteadmin pages with the function siteAdmin.reloadPages() only after the storing of the pages porperties CQ.form.SlingSubmitAction(dialog.form, config); is executed sucessfully.

the following function submits a form and then calls a callback() function, which in turn reloads the siteadmin pages:

function foo(dialog, config, callback) {
    var action = new CQ.form.SlingSubmitAction(dialog.form, config);
    dialog.form.doAction(action);
    dialog[dialog.closeAction]();

    callback();
}

function call:

foo(dialog,action, function() {
    var siteAdmin = CQ.Ext.getCmp(window.CQ_SiteAdmin_id);
    siteAdmin.reloadPages();
});

The problem is that the siteadmin pages are not reladed after the pageProperties ( dialog.form ) has been successfully submitted. For example the pageTitle loaded is the old value.

How to reload siteadmin pages only after the form has been successfully submitted?

You can try to specify the success function in the config, something like the one shown below.

function foo(dialog, config, callback) {
    var action = new CQ.form.SlingSubmitAction(dialog.form, {
        success: function(){
            var siteAdmin = CQ.Ext.getCmp(window.CQ_SiteAdmin_id);
            siteAdmin.reloadPages();
        }
    });
    dialog.form.doAction(action);
    dialog[dialog.closeAction]();
}

This would ensure that the siteadmin is reloaded only after you request succeeds.

Check the further options available for your config here .

You should call the callback of foo from success callback of CQ.form.SlingSubmitAction

function foo(dialog, config, callback) {
    var action = new CQ.form.SlingSubmitAction(dialog.form, 
       {
         params: config.params,
         success: function(frm, resp) {
           callback();
         }
       });
    dialog.form.doAction(action);
    dialog[dialog.closeAction]();
}

I don't see any sort of test for whether or not the form-submission was successful. You could simply avoid calling the callback() function until after such a test has been passed. Or, you could put the test inside the callback() function, and not do the page-reload until after the test has been passed. OR, you can estimate how long it takes for the form-submission to be transmitted to the Web Server, and how long it takes the Server to process the data, and then do something like this:

 setTimeOut("callback();", 500); //wait 500 milliseconds (sample estimate)

OR

 setTimeOut("siteAdmin.reloadPages();", 500); //inside the callback() function

Note this assumes the submitted data is always successfully processed, but using setTimeout() is most certainly a generic way to ensure one function is DONE, before another gets called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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