简体   繁体   English

仅在其他功能的执行完全完成后,如何调用功能

[英]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); 在下面的代码中, 仅在存储了页面属性CQ.form.SlingSubmitAction(dialog.form, config); 之后 ,我才想使用功能siteAdmin.reloadPages()重新加载siteadmin页面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: 以下函数提交表单,然后调用callback()函数,该函数反过来重新加载siteadmin页面:

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. 问题是成功提交pageProperties( dialog.form )后,不会对siteadmin页面进行更改。 For example the pageTitle loaded is the old value. 例如,加载的pageTitle是旧值。

How to reload siteadmin pages only after the form has been successfully submitted? 仅在成功提交表单后,如何重新加载siteadmin页面?

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. 这样可以确保仅在请求成功后才重新加载siteadmin。

Check the further options available for your config here . 此处检查可用于您的配置的其他选项。

You should call the callback of foo from success callback of CQ.form.SlingSubmitAction 您应该从CQ.form.SlingSubmitAction的成功回调中调用foo的回调

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. 您可以简单地避免调用callback()函数,直到通过这样的测试为止。 Or, you could put the test inside the callback() function, and not do the page-reload until after the test has been passed. 或者,您可以将测试放入callback()函数中,直到通过测试后才进行页面重新加载。 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: 或者,您可以估计将表单提交传输到Web服务器需要多长时间,以及服务器处理数据需要多长时间,然后执行以下操作:

 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. 请注意,这假定提交的数据始终可以成功处理,但是使用setTimeout()无疑是一种确保在调用另一个函数之前完成一个函数的通用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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