表单提交调用适配器

时间:2013-10-02 16:02:25

标签: ibm-mobilefirst worklight-adapters

我有一个调用过程onsubmit的表单,此过程解析文档并创建一个传递给适配器的json对象。似乎当onSubmit过程结束时,对适配器的调用被终止,然后调用适配器的onFailure方法。

我的问题是如何在onSubmit程序中等待适配器完成。

如果我在onSuccess中添加一个标志并等到标志设置,我将无法捕获真正的故障。如果我在onFailure中添加一个标志,因为onFailure被调用因为进程被终止,我将无法等待该进程的结束。

如果我在onSubmit过程中调用适配器后添加一个警报并等待触发onSuccess,那么它是有效的...

这里有一些代码:

function postCustomer(content) {
    var invocationData = {
        adapter : 'myAdapter',
        procedure : 'postCustomerByContent',
        parameters : [ content ]
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : postCustomerSuccess,
        onFailure : postCustomerFailure,
        timeout: 30000
    });
}

function postCustomerSuccess(result) {
    var httpStatusCode = result.status;
    if (200 == httpStatusCode) {
        var invocationResult = result.invocationResult;
        var isSuccessful = invocationResult.isSuccessful;
        if (true == isSuccessful) {
            WL.SimpleDialog.show('Title', "Success", [{text : 'OK'}]);
        } else {
            WL.SimpleDialog.show('Title', "Error. isSuccessful=" + isSuccessful, [{text : 'OK'}]);
        }
    } else {
        WL.SimpleDialog.show('title', "Error. httpStatusCode=" + httpStatusCode, [{text : 'OK'}]);
    }
}

function postCustomerFailure(result) {
    WL.SimpleDialog.show('Title', "Failed:"+result, [{text : 'OK'}]);
}

function formSubmit() {
    var application = document.forms["application"], initial = application["ibmerName"].value, email, name, organizationName = application['organizationName'].value, primaryContactName = application['primaryContactName'].value, primaryContactEmail = application['primaryContactEmail'].value, organizationAddress = application['organizationAddress'].value, primaryContactPhoneNumber = application['primaryContactPhoneNumber'].value, country = application['country'].value, organizationType = application['organizationType'].value;
    if (initial == "xxx") {
        email = "xxxxxxxxxxxxxxxxx";
        name = "xxx";
    } else if (initial == "yyy") {
        email = "yyyyyyyy";
        name = "yyyyyyyyyy";
    } else {
        email = "xxxxxxxxxxxxxxxxxx";
        name = "xxxxxxxxxxxxxxxxx";
    }
    var content = '{"email":"' + email + '","name":"' + name
            + '","organizationName":"' + organizationName
            + '","primaryContactName":"' + primaryContactName
            + '","primaryContactEmail":"' + primaryContactEmail
            + '","organizationAddress":"' + organizationAddress
            + '","primaryContactPhoneNumber":"' + primaryContactPhoneNumber
            + '","country":"' + country + '","organizationType":"'
            + organizationType + '"}';
    postCustomer(content);

    alert(content);

}

有什么想法吗? THX

1 个答案:

答案 0 :(得分:0)

是刷新页面,如果是这样,你必须在formSubmit函数结束时返回false。