什么是Backbone.js的做与那些不再使用的模型 [英] What does backbone.js do with models that are not used anymore

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

问题描述

最近,我潜水到整个客户端的MVC / MVVM设计paterns,一来我特别感兴趣的是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.

推荐答案

骨干并没有明确处理清理对象为您服务。这是50/50你和JavaScript运行。

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中使用删除关键字的东西:删除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

骨干的JavaScript,因此属于相同的规则之下。还有,你需要知道的,这将帮助你知道当你需要手动清理一些对象骨干封锁和引用的一些有趣用途。

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.

例如:事件。甚至处理器/回调方法的工作原理是具有触发事件的对象,处理该事件的回调之间的引用。这是最简单的地方导致内存泄漏中的骨干应用程序之一,我详细,在这里讨论一下:<一href=\"http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/\">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中管理内存,你会没事的。一旦你要删除的用户对象的该集合的所有引用,他们将被清理。

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天全站免登陆