简体   繁体   English

执行Knockout.js数据绑定:在href跳转到另一个html元素之前单击click函数

[英]Execute Knockout.js data-bind : click function before href jumps to another html element

I try to execute a function with binded with knockouts data-bind="click : yourFunction" and want it to be executet before the href inside the same tag jumps to antoher view. 我尝试执行与敲除绑定的函数data-bind =“ click:yourFunction”,并希望在同一标记内的href跳到另一个视图之前执行该函数。 How can I do this? 我怎样才能做到这一点? Code example: 代码示例:

<a data-role="detailbutton" data-icon="location"
data-bind="click: $parent.getLatLong"     
style="margin-right:45px;" href="#map-view" ></a>

One thing you can do is prevent default in your click handler and do the navigation to the new view yourself: 您可以做的一件事是阻止点击处理程序中的默认设置,然后自己导航到新视图:

(Pseudocode) (伪码)

parent.getLatLong = function(evt) {
  evt.preventDefault();
  /* do my stuff */
  window.location.href = '#map-view';
}

Obviously, if you have to do asynchronous stuff (as i suspect you do) you need to navigate on the response callback of that async call. 显然,如果您必须执行异步操作(如我所怀疑的那样),则需要在该异步调用的响应回调上进行导航。

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

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