knockout js best practices ajax error handling

后端 未结 2 559
情书的邮戳
情书的邮戳 2021-02-07 21:37

What best practices for Knockout js have you used when ajax json responses come back bad.

How do you create your mappings to display an error has occured to the user? Ho

2条回答
  •  礼貌的吻别
    2021-02-07 22:07

    Personally I would only update the knockout models upon success. This then leaves the models in the same state as prior to the error.

    In the case of an error, you could do anything you want to tell the user there has been an error. I would use humane.js to show a message to the user that the update or creation has failed for some reason. In your three cases for model error, no response or server error, you could check the state of the post in the error function and choose a different message to display to the user

    They then have the freedom to do the same thing again, or correct their mistake and try again.

    $.post("somewhere")
    .success(function() {
        // update knockout models here
    })
    .error(function() {
        // show error message to user
    })
    .complete(function() {
        // reset any temporary variables
    });
    

提交回复
热议问题