简体   繁体   English

如何删除客户端的骨干模型?

[英]How to delete a backbone model client-side?

What's the best way to delete a model client side? 删除模型客户端的最佳方法是什么? I don't need to worry about removing it server-side. 我不需要担心在服务器端删除它。 How do I make sure it is removed everywhere, avoiding every gotcha, every zombie binding. 我如何确保它被移除到处,避免每个陷阱,每个僵尸绑定。 I'm looking for a guide to removing and destroying everything and ensuring the model is garbage collected. 我正在寻找删除和销毁所有内容并确保模型被垃圾收集的指南。

Thanks!! 谢谢!!

It really depends on what is inside this model. 这真的取决于这个模型里面的内容。 If it is binded to events from other instances - View/Collection/Models, you should remove those event listeners manually, since there is no way to remove all of them at once. 如果它绑定到来自其他实例的事件 - 视图/集合/模型,则应手动删除这些事件侦听器,因为无法一次删除所有这些事件。

Also, Model.destroy() removes the model from any collections ( backbone documents ) : 此外,Model.destroy()从任何集合(骨干文档)中删除模型:

Destroy model.destroy([options]) 销毁model.destroy([options])

... Triggers a "destroy" event on the model, which will bubble up through any collections that contain it ... ...在模型上触发“销毁”事件,该事件将通过包含它的任何集合冒泡......

The thing that you might want to do is assign a new destroy method which includes the event triggering and the stuff you want to remove. 您可能想要做的是分配一个新的destroy方法,其中包括事件触发和要删除的内容。

destroy: function(options) {
   // Any events you wish to switch off ( if you have any )
   SomeCollection.off('change', this.changeFn);

   Backbone.Model.prototype.destroy.apply(this, options);       
}

May be you should also be aware of some patterns for making less garbage from Models : 也许您还应该了解一些模型中减少垃圾的模式:

  1. Don't place your initialized model in a variable ( keep it in the collection ); 不要将初始化的模型放在变量中(将其保存在集合中);
  2. Make sure you write your code in a way that no events are binded from the Model ( Use views/collections for that ); 确保编写代码的方式是没有事件与模型绑定(使用视图/集合);
  3. Keep your model code simple, since models in your app will be most numbered. 保持您的模型代码简单,因为您的应用程序中的模型将编号最多。

I think by following those rules you won't need to worry so much about garbage from your Models. 我认为通过遵循这些规则,您不必太担心模型中的垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM