How to perform a Knockout.js action before followi

2019-08-29 10:48发布

问题:

I came across this issue while working with knockout.js and Html5, I haven't found how to handle it, though. In here, what matter the most is performing a click-binding right before following an Href to another page. The binding itself has an ajax method to insert data into my DB so before following the stated href it's mandatory to perform this binding.

HTML goes like this:

       <div data-role="header" data-theme="b">  
            <a href="secondForm.html" data-icon="back" rel="external"> Back </a>
            <h1> Some Text </h1>  

            <div  class="ui-btn-right"  data-role="controlgroup" 
                   data-type="horizontal">

                 <a data-bind="click: linkCustomer" rel="external"  id="continue" 
                  data-role="button" data-theme="b" href="home.html">Continue</a>

            </div>
        </div>

It's thought that both actions would be performed, but that belief is wrong. The only action which is performed is the Href.

Do I have any error within my code?

回答1:

That's a classical case.

You try to execute some js scripts, but in the same time, your browser is redirecting to the href.

To fix this, if you are using angular, you can use promises; otherwise, you have to stop the event propagation, process to you stuff, and then, redirect to the link.href attribute when done.



回答2:

Its performed client side, test my fiddle here

http://jsfiddle.net/zXWTW/1/

My guess is the ajax request does not start before the page is unloaded