替换Knockout.js observableArray中的所有元素 [英] Replace all elements in Knockout.js observableArray

查看:87
本文介绍了替换Knockout.js observableArray中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有一个 observableArray 。创建vm后,我希望完全替换 observableArray 的数据。我是这样做的:

I have an observableArray in my view model. After creating the vm I wish to completely replace the data the observableArray. Here's how I'm doing it:

//Initial Setup
var vm = {};
vm.roles = ko.observableArray([]);
ko.applyBindings(vm); 


//....replace array later on....
vm.roles(["1", "2"]);

这似乎工作正常,但我担心这是不正确的,可能会导致内存泄漏。如果这是更新现有 observableArray 的首选方式,假设您希望替换其所有数据,那么任何人都可以遵守吗?

This seems to be working fine, but I was concerned if this was incorrect and might lead to memory leaks. Can anyone conform if this is the preferred way to update an existing observableArray assuming you wish to replace all its data?

我注意到 observableArray 确实有 removeAll()方法,并想知道是否需要调用这个干净利落,或者我对我正在做什么感觉良好?

I noticed observableArray does have a removeAll() method and wondered if that needed to be called to do this cleanly, or if I'm fine with what I'm doing?

推荐答案

您正在使用的技术是完全替换 observableArray 中的数据的推荐方法。 observableArray 实际上只是一个普通的 observable ,为有用的数组操作添加了额外的函数,这些操作作用于底层数组并触发通知。

The technique that you are using is the recommended approach for completely replacing the data in an observableArray. An observableArray is actually just a normal observable with extra functions added for useful array operations that act on the underlying array and trigger notifications.

这篇关于替换Knockout.js observableArray中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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