如何在淘汰视图模型单击事件中访问$ parent或$ parents []? [英] How to access $parent or $parents[] in knockout viewmodel click event?

查看:35
本文介绍了如何在淘汰视图模型单击事件中访问$ parent或$ parents []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我想通知子视图模型中发生的单击事件,其祖父母或$ parents [1].所以基本上我想能够做到这一点

I've got a situation where I'd like to notify a grandparent or $parents[1] of a click event that occurs in a sub viewmodel. So basically I'd like to be able to do this

self.$parents[1].actionTaken

由于绑定上下文与viewModel的关系,我认为这行不通,但我想听听是否有人对执行此类操作的正确方法有任何想法.谢谢

I think this doesn't work because of binding context vs viewModel but I'd like to hear if anyone has an ideas on the correct way to do something like this. Thanks

 self.save = function () {

     //do stuff to self first, then

      self.$parents[1].actionTaken();

};

推荐答案

或者,您可以通过以下方式将参数传递给视图模型的事件处理程序: 更改您的save方法

Alternatively, you can pass parameters to your view model's event handler the following way: Change your save method

self.save = function(theParents) {
    theParents[1].actionTaken();
}

并通过以下方式绑定您的处理程序:

and bind your handler the following way:

data-bind="click: function() { $data.save($parents) }"

有关更多信息,请参阅点击处理程序的文档中的注2 (您需要对位)

For further information, please refer to Note 2 in the click handler's documentation (you need to scoll down a bit)

这篇关于如何在淘汰视图模型单击事件中访问$ parent或$ parents []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆