backbone.js 对不再使用的模型做了什么 [英] What does backbone.js do with models that are not used anymore

查看:20
本文介绍了backbone.js 对不再使用的模型做了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在深入研究整个客户端 MVC/MVVM 设计模式,我特别感兴趣的一种是backbone.js.

Lately I am diving into the whole client-side MVC/MVVM design paterns, and the one I am particularly interested in is backbone.js.

我不完全理解的一件事是当模型不再真正需要时会发生什么.

One thing I do not fully understand is what happend to the models when they are not really needed anymore.

假设我们有一个包含用户和产品的应用.我们有用户模型/视图和产品模型/视图

Let's say we have an app that has users and products. We have user models/views and product models/views

注意:为简单起见我们不是用户.我们可以只 CRUD 用户/产品.

NOTE: for simplicity sake we are not a user. We can just CRUD users / products.

当我进入产品页面时,我假设我们加载了模型和与之对应的视图.

When I enter the products page, I assume we load the model and the view corresponding to this.

当我们离开页面并进入用户页面时会发生什么.加载了用户模型/视图,但仍加载了产品.

What happens when we leave the page and enter the users page. A user model/view is loaded, but the products are also still loaded.

我们是否让它们保持加载状态,主干是否会为您处理这些问题,或者您是否明确需要结束某些对象.

Do we keep them loaded, does backbone take care of that for you, or do you explicitly need to end certain objects.

推荐答案

Backbone 不会为您明确处理清理对象.您和 JavaScript 运行时 50/50.

Backbone does not explicitly handle cleaning up objects for you. It's 50/50 you and the JavaScript runtime.

JavaScript 是一种垃圾收集语言,如 Java、C#、Ruby 等.垃圾收集语言的基本原理是您的应用程序仍然引用的对象不会被清除.与之相反的是,当一个对象不再被您的应用程序引用时,将被清除.

JavaScript is a garbage collected language like Java, C#, Ruby, and others. The basic of a garbage collected languages is that an object that is still referenced by your application will not be cleaned up. The counter to that is when an object is no longer referenced by your application, will be cleaned up.

当您创建一个变量时,您可以将该变量的范围限定为局部函数或作为全局变量.

When you create a variable, you can either scope that variable to a local function or as a global variable.

在页面的生命周期中,垃圾收集器永远不会清除全局变量.唯一一次清理它们是当您完全将 HTML 页面留在后面时 - 导航到不同的页面并强制浏览器从服务器加载新页面(执行完整的服务器刷新)或关闭浏览器或浏览器选项卡.

Global variables are never cleaned up by the garbage collector, during the life of the page. The only time they are cleaned up is when you leave the HTML page behind entirely - navigate to a different page and force the browser to load the new page from the server (doing a complete server refresh) or closing the browser or browser tab.

函数作用域变量在变量超出作用域时被清除——也就是说,当函数退出并且不再有对它的引用时.有一些例外:返回值和闭包.

Function scoped variables are cleaned up when the variable falls out of scope - that is, when the function has exited and there are no more references to it. There are a few exceptions to this: return values and closures.

通过将返回值分配给另一个变量,将返回值保存在您的应用中.返回值遵循相同的一般规则,但变量现在位于不同的函数中.一旦该变量超出范围,就可以清除它.

A return value is held in your app by assigning the return value to another variable. A return value falls under the same general rules, but the variable is now in a different function. Once that variable goes out of scope, it can be cleaned up.

闭包允许父作用域提供后代作用域可以访问的值.当后代作用域被清理时,父级的闭包变量可能被允许被清理(假设没有其他东西保留它).

A closure allows a parent scope to provide values that a descendant scope can access. When the descendant scope is cleaned up, the parent's closured variable may be allowed to be cleaned up (assuming nothing else is holding on to it).

具有属性和功能的对象属于相同的规则.一个对象可以通过为其分配一个属性来引用另一个对象或函数:myObj.foo = thatObj.

Objects with attributes and functions fall under the same rules. An object can reference another object or function by having an attribute assigned to it: myObj.foo = thatObj.

DOM(文档对象模型 - 您应用中的 HTML)是一个 JavaScript 对象.对 DOM 的事件和其他引用的工作方式与任何其他引用相同.如果你有一个处理 DOM 事件的对象,它在你的应用程序中有一个引用,垃圾收集器不会清理它.如果你想清理它,你必须删除所有对它的引用——包括来自事件处理程序的 DOM 引用.

The DOM (Document Object Model - the HTML in your app) is a JavaScript object. Events and other references to your DOM work the same as any other reference. If you have an object handling a DOM event, it has a reference in your app and it won't be cleaned up by the garbage collector. If you want it cleaned up, you have to remove all references to it - including the DOM reference from the event handler.

一般规则是,如果您将数据加载到主干集合或对象中,并且您希望清除该对象以使其不再使用内存,则必须删除对该对象的所有引用.这只是标准的 JavaScript 垃圾回收规则.

The general rule is that if you are loading data in to a backbone collection or object and you want that object to be cleaned up so it's not using anymore memory, you must remove all references to that object. This is just the standard JavaScript garbage collection rule.

你不能强制垃圾回收,但你可以强制一个变量去引用它指向的东西,在 JavaScript 中使用 delete 关键字:delete myVar

You cannot force garbage collection, but you can force a variable to de-reference the thing it points to using the delete keyword in JavaScript: delete myVar

Backbone 是 JavaScript,因此它属于相同的规则.在 Backbone 中有一些有趣的闭包和引用用法需要注意,这将有助于您了解何时需要手动清理某些对象.

Backbone is JavaScript so it falls under the same rules. There are some interesting uses of closures and references in Backbone that you need to be aware of, which will help you to know when you need to manually clean up some objects.

例如:事件.偶数处理程序/回调方法通过在触发事件的对象和处理事件的回调之间引用来工作.这是在 Backbone 应用程序中最容易导致内存泄漏的地方之一,我在这里详细讨论:http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

For example: events. An even handler / callback method works by having a reference between the object that triggers the event and the callback that handles the event. This is one of the easiest places to cause memory leaks in a Backbone app and I discuss it in detail, here: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

除了了解事件在引用方面的工作方式之外,只需遵循 JavaScript 中管理内存的标准规则,您就可以了.删除对该 User 对象集合的所有引用后,它们将被清除.

Other than being aware of how events work in terms of references, just follow the standard rules for manage memory in JavaScript and you'll be fine. Once you remove all references to that collection of User objects, they'll be cleaned up.

这篇关于backbone.js 对不再使用的模型做了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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