这并不引用 observableArray 模板绑定中的 viewmodel

发布于 2025-01-04 22:26:20 字数 1049 浏览 0 评论 0原文

尝试使用新的更简洁的事件处理在 Knockout 2 中以及新的控制流绑定中,我正在致力于实现一个简单的动态列表,添加和删除字符串。所以我的标记大致如下所示:

<div data-bind="foreach:myList">
    <input data-bind="value: $data" />
    <button data-bind="click:$parent.removeFromList">X</button>
</div>

我的视图模型有一个匹配的删除函数,它模仿 来自 Steve Sanderson 的示例

removeFromList: function(item) {
    this.myList.remove(item);
}

现在我希望“this”引用视图模型,项目引用被删除的数组成员(因为 事件处理程序现在接收当前模型值作为其第一个参数)。然而,“this”似乎也指被删除的字符串。因此,当我单击“删除”时,我得到:

this.myList is undefined

我在 http://jsfiddle.net/davidc/rFd7H 创建了一个 JSFiddle / 这说明了我的问题。我应该如何从列表中删除项目?

Trying to use the new cleaner event handling in Knockout 2 along with the new control flow bindings I'm working on implementing a simple dynamic list, adding and removing strings. So my markup roughly looks like this:

<div data-bind="foreach:myList">
    <input data-bind="value: $data" />
    <button data-bind="click:$parent.removeFromList">X</button>
</div>

and my viewmodel has a matching remove function which immitates a sample from Steve Sanderson.

removeFromList: function(item) {
    this.myList.remove(item);
}

Now I would expect 'this' to refer to the viewmodel and item to refer to the array member being removed (since event handlers now receive the current model value as their first parameter). However, 'this' also seems to refer to the string being removed. Therefore when I click remove I get:

this.myList is undefined

I've created a JSFiddle at http://jsfiddle.net/davidc/rFd7H/ which illustrates my problem. How should I be removing items from the list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2025-01-11 22:26:20

有多种方法可以确保您的处理程序具有正确的“this”值。

  1. 在函数中构建视图模型,并对当前 this 使用 bind 示例:http://jsfiddle.net/rniemeyer/rFd7H/4/。或者不要在函数中构建视图模型并使用 bind ,例如: http ://jsfiddle.net/rniemeyer/rFd7H/10/

  2. 在函数中构建视图模型,并保留一个具有正确值 this 的变量,并在您的处理程序。示例:http://jsfiddle.net/rniemeyer/rFd7H/5/

  3. 绑定绑定中的正确上下文,例如:data-bind="click: $parent.removeFromList.bind($parent)" 示例:http://jsfiddle.net/rniemeyer/rFd7H/8/

  4. 以匿名身份调用$parent 对象的函数,例如: data-bind="function() { $parent.removeFromList($data); }" 一般来说,不推荐,因为它使标记变得丑陋/冗长。示例:http://jsfiddle.net/rniemeyer/rFd7H/9/

There are many ways that you can ensure that your handler has the correct value for "this".

  1. Build your view model in a function and use bind against the current this Sample: http://jsfiddle.net/rniemeyer/rFd7H/4/. Or don't build your view model in a function and use bind like: http://jsfiddle.net/rniemeyer/rFd7H/10/

  2. Build your view model in a function and keep a variable with the correct value of this and use it in your handler. Sample: http://jsfiddle.net/rniemeyer/rFd7H/5/

  3. Bind to the correct context within your binding like: data-bind="click: $parent.removeFromList.bind($parent)" Sample: http://jsfiddle.net/rniemeyer/rFd7H/8/

  4. Call it as an anonymous function off of the $parent object like: data-bind="function() { $parent.removeFromList($data); }" Generally, not recommended, as it makes the markup ugly/verbose. Sample: http://jsfiddle.net/rniemeyer/rFd7H/9/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文