用于点击事件绑定的淘汰通道$parent
Knockout pass $parent for click event binding
我有这个代码:
<div id="menu">
<ul data-bind="foreach: elements">
<li>
<span data-bind="text:domObj().tagName +' [' + classOriginal() + ']', click: $root.elementClick"></span>
<ul class="menuclassitem" style="display:none" data-bind="foreach: classFixes">
<li data-bind="text:$data, click:$root.classClick.bind($parent)"></li>
</ul>
</li>
</ul>
</div>
如您所见,我正在尝试将$parent对象("elements"数组中的当前项)传递给 $root.classClick 函数,但实际传递的值是当前内部循环的$data(当前"classFixes"数组的$data)
有没有人知道如何在classClick函数中使用$parent?
相当化石的问题,但它仍然可能对某人有所帮助。
这个问题更多地与.bind()
的方式有关,而不是与KnockoutJS有关。所以首先我们需要了解 bind() 是如何工作的:
bind() 方法创建一个新函数,该函数在调用时具有其 此关键字设置为提供的值,具有给定的序列 调用新函数时提供的任何参数前面的参数。更多。。。
现在让我们回到问题。默认情况下,KnockoutJS 将当前$data
作为click
绑定接收的函数的第一个参数传递。将同一$data
用作默认上下文。在@DorR编写click:$root.classClick.bind($parent)
单击绑定后,单击绑定收到了一个具有重新定义的上下文($parent)和预定义参数的函数,这允许KO将$data作为第一个参数传递。 $parent现在可以在classClick
中作为this
访问。
为了说明它是如何工作的,我在下面做了这个小片段。尝试单击不同列中的按钮并检查下面的调试日志,以查看KO传递的参数如何被.bind()
预定义的参数向右移动
function VM() {
const self = this;
self.debug = ko.observable('');
self.catalog = [
{
name:'Section1',
items:[
{id:1,name:'Item1'},
{id:2,name:'Item2'},
]
},
{
name:'Section2',
items:[
{id:4,name:'Item4'},
]
}
];
self.orderClick = function(arg1,arg2,arg3,arg4) {
let dbgText = "this:<br> ";
dbgText += JSON.stringify(this);
dbgText += "<br>1st arg:<br> ";
dbgText += JSON.stringify(arg1);
dbgText += "<br>2nd arg:<br> ";
dbgText += JSON.stringify(arg2);
dbgText += "<br>3nd arg:<br> ";
dbgText += JSON.stringify(arg3);
dbgText += "<br>4th arg:<br> ";
dbgText += JSON.stringify(arg4);
self.debug(dbgText);
}
}
ko.applyBindings(new VM())
table {
border-collapse: collapse;width:100%;
}
td {
border:1px solid #CCC;
text-align:center;
font-family:sans-serif;
font-size:11px;
white-space:nowrap;
}
button {
display:block; margin:5px auto;
}
.dbglog {
padding:5px 10px;
border:1px solid #AAA;
background-color:#EEE;
font-family:"Lucida Console", Monaco, monospace;
font-size:12px;min-height:50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table data-bind="foreach: catalog">
<tr>
<td colspan=10><b data-bind="text:$data.name"></b></td>
<td>
<table data-bind="foreach:$data.items" class=nested>
<tr>
<td width=10%>
<i data-bind="text:$data.name"></i>
</td>
<td width=30%>
no .bind()
<button data-bind="click:$root.orderClick">click</button>
</td>
<td width=30%>
.bind($parent)
<button data-bind="click:$root.orderClick.bind($parent)">click</button>
</td>
<td width=30%>
.bind($parent,$data,'smthelse')
<button data-bind="click:$root.orderClick.bind($parent,$data,'text')">click</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div class=dbglog data-bind="html:$root.debug"></div>
希望这个答案能帮助某人。 =)
classClick 函数中的 'this' 变量是 $parent。传递给 classClick 的第一个参数是 $data。这把小提琴演示
ko.applyBindings({
elements: [
{
classFixes : ["ab","cd"]
},
{
classFixes : ["ef","gh"]
}
],
classClick : function(first)
{
alert(JSON.stringify(this));
alert(JSON.stringify(first));
}
});
- 淘汰搜索/筛选
- 消息显示之外的淘汰验证
- 淘汰映射;不起作用
- 如何正确引用传递到淘汰中的当前元素的属性.JS
- 如何使用RGB或HSL显示HWB/HSB/CMYK通道
- 什么是淘汰赛中的根管理员.js以及它指向什么
- 可排序和可丢弃的淘汰列表
- GoogleCalendarJavaScriptapi,将用户添加到带有“;写“;通道
- 条件显示淘汰js
- 淘汰,复选框不起作用
- 如何使用淘汰从下拉列表中获取选定值
- 在淘汰赛中,我能得到可观测者的名字吗
- 使用AMD时未定义淘汰组件视图模型
- 淘汰JS内联三元
- Kurento数据通道的创建和管理
- 保存淘汰函数时如何处理原型
- 淘汰从选项中抓取类
- 使用淘汰缓存计算值
- 自定义过滤淘汰js observableArray
- 用于点击事件绑定的淘汰通道$parent