如何在淘汰赛视图中引用此内容 [英] How reference this in knockout view
问题描述
问候,我今天有一个非常简单的问题要问你.
Greetings all I have a very simple question for you today.
我有这个标记(视图):
I have this markup (view):
<div class="cards" data-bind="sortable: { data: cards, afterMove: cardMoved }">
<div class="card">
<div><span data-bind="text: title"></span></div>
<div class="dnnRight" data-bind="if: firstMatch(this, id)">
<a href="#" class="dnnSecondaryAction" data-bind="click: $root.openCard"><%= LocalizeString("EditBtn") %></a>
<a href="#" class="dnnSecondaryAction" data-bind="click: $parent.removeCard"><%= LocalizeString("DeleteBtn") %></a>
</div>
</div>
</div>
基本上,它在 card
模型上具有 foreach
数据绑定.我已经编写了一个足够简单的函数,可以根据卡模型定义中的 id
参数找到第一个匹配元素.但是我需要能够引用有问题的卡,以便在我的函数中访问它的可观察参数.
Basically it has a foreach
data-binding on card
model. I have written a simple enough function to find first matching element based on id
parameter which lives inside card model definition. But I need to be able to refer to the card in question in order to access it's observable parameters within my function.
如何参考个人卡?此
关键字不适合我.
How can I reference individual card? this
keyword isn't working for me.
正如我指出的那样,我一直在尝试敲除文档绑定上下文,但是大多数情况下,我都会遇到错误.
As I pointed out I've been trying everything from knockout documentation Binding context but most of the time I end up with an error.
Error: Unable to process binding "foreach: function (){return lists }" Message: Unable to process binding "sortable: function (){return { data:cards,afterMove:cardMoved} }" Message: Unable to process binding "if: function (){return firstMatch($data,2) }" Message: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.
更新2
实际上,如果在 if
调用 firstMatch()
时我将函数调整为仅以 id
作为参考,这可能会有所帮助能够使用 this.property
Update 2
Actually it might help if I adjust my function to just take id
as a reference becuase when if
calls firstMatch()
I should be able to access members
ìn the current context using this.property
//identify the first matching item by id
self.firstMatch = ko.computed(function (id) {
var search = false;
ko.utils.arrayForEach(self.members, function (member) {
if (member.id == id) {
search = true;
}
});
return search;
});
推荐答案
检查 self.members()是否具有值.如果为null,则不会循环
Check whether self.members() have values or not. If it is null it will not go in the loop
这篇关于如何在淘汰赛视图中引用此内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!