Search code examples
jqueryhtmlknockout.jsform-submitstoppropagation

Stop action propagation within a form using Jquerymobile, Knockout.js?


So, I have this issue about performing two actions at the very same time. One of the actions is an AJAX call which consumes a webservice and do some stuff to save data on the server, the other action is to return to a view where there's a list.

My code just follows the href but doesn't perform the binding SUBMIT. I was told that to do what I want I need to "stop the propagation of the action" but I don't know how. Would you mind giving me some tips? Here's the code:

<form data-bind="submit: updateData" method="post" data-ajax="false">
  <div data-role="content" data-theme="d">
   <center>
     <h2> Save changes? </h2>
     <input type="submit" name:"submit" data-inline="true">
   </center>
  </div>
</form>

This question is based upon this one -> How to perform a Knockout.js action before following an <Href>? I was doing in a different way before but then I changed it to a form. However, none of these so worked out to me.


Solution

  • You can do it like this

    <form data-bind="submit: updateData" method="post" data-ajax="false">
        <div data-role="content" data-theme="d">
            <center>
                <h2> Save changes? </h2>
                <input type="submit" name:"submit" data-inline="true">
            </center>
        </div>
    </form>
    
    self.UpdateData = function(){
        if(self.AjaxRequest()){
            //do other action
            //  $('some id').trigger('click');
        }
    }
    self.AjaxRequest = function(){
        var status = false
        $.jax({
            ...,
            success : function(){
                status = true
            }
        })
        return status
    }