Backbone.js的:不调用collection.reset()删除车型呢? [英] backbone.js: does calling collection.reset() removes the models as well?

查看:113
本文介绍了Backbone.js的:不调用collection.reset()删除车型呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的骨干文档调用 collection.reset()清除集合中读出。我想知道,如果它消除了模型和还是他们继续生活在记忆?

如果他们不删除有删除一个集合中的所有车型,而无需通过模型迭代,并调用 model.remove()

解决方案

你所寻找的是,可能是,为模特进行垃圾回收。也就是说,没有人对这些车型的引用了,它们是从集合中移除后。

骨干确实去除它的模型设定,当它们从集合中删除的参考文献其一部分。但是,你必须做你自己的清理,如果你的code的这些模型的引用。在大多数情况下,这种情况如果这些模型被注册为事件侦听器,就像这个例子:的http:/ /jsfiddle.net/dira/4uxp4/2/

的实施复位 _removeReference ,你可以改变调用模型上的清理功能以及。而在模型中,所有听众/所有其他对象保持对它的引用删除模式。

I read in the backbone documentation that calling collection.reset() clears the collection. I want to know if it removes the models as well or do they continue to live in memory?

If they're not removed is there an easier way to remove all the models in a collection without iterating through the models and calling model.remove()?

解决方案

What you are looking for is, probably, for the models to be garbage-collected. That is, that nobody has a reference to these models anymore, after they are removed from the collection.

Backbone does its part of removing the references that it set on the models, when they are removed from the collection. However, you have to do your own cleanup if your code has references to those models. Most of the time, this happens if those models are registered as event listeners, like in this example: http://jsfiddle.net/dira/4uxp4/2/

Taking a look at the implementation of reset, you could change _removeReference to call a cleanup function on the model as well. And in the model, remove the model from all the listeners/all the other objects that keep a reference to it.

这篇关于Backbone.js的:不调用collection.reset()删除车型呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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