扩展敲除可观察数组 [英] Extending knockout observable array

查看:71
本文介绍了扩展敲除可观察数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向ko.observableArray()添加一些类似"ArrayCollection"的功能;

I'd like to add some "ArrayCollection" like functionality to a ko.observableArray();

即 removeItemAt(index)//调度项目已删除事件 addItem(item)//调度项目添加事件 等等

i.e removeItemAt(index)//dispatches item removed event addItem(item)//dispatches item added event etc

在大多数ko示例中,我注意到这是在模型中处理的.我有一个带有类型化集合的相当丰富的模型,因此想将其嵌套在集合/数组本身中.

I notice in most of the ko examples this is handled in the model. I have a fairly rich model with typed collections so would like to nest this in the collection/array itself.

这种方法适合ko吗?

推荐答案

扩展observableArrays既简单又完全合理.

Extending observableArrays is easy and a perfectly reasonable thing to do.

最简单的方法是将功能添加到ko.observableArray.fn.现在有一个文档页面.

The easiest way to accomplish this is to add functions to ko.observableArray.fn. There is now a documentation page for this technique.

这与您追求的有点不同,但是这是您可能会感兴趣的ko字典的一个很好的实现:

This is a little bit different than what you are after, but here is a nice implementation of a dictionary in ko that may be of interest to you: https://github.com/jamesfoster/knockout.observableDictionary

这篇关于扩展敲除可观察数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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