简体   繁体   English

Backbone.js视图中究竟是什么this.el?

[英]What exactly is this.el in Backbone.js views?

I'm confused about the meaning of el in Backbone views. 我对Backbone视图中el的含义感到困惑。 Some tutorials (such as this one ) make it seem like el is the parent of the view being created, where the render method is supposed to append the view to its el . 一些教程(例如这个教程)使得它看起来像el是正在创建的视图的父级 ,其中render方法应该将视图附加到其el Others (like this ) treat it like it's the element of the view itself. 其他人(像这样 )把它视为视图本身的元素。 Is there a best practice concerning this? 有关于此的最佳做法吗?

el is definitely the element the view corresponds to, not its parent. el绝对是视图对应的元素,而不是其父元素。 In your first example, they considering the whole page body to be the view, and then upon rendering, they insert the components that compose it. 在您的第一个示例中,他们将整个页面body视为视图,然后在渲染时,它们插入组成它的组件。 I suppose this might be appropriate for top-level views representing entire pages. 我想这可能适合代表整个页面的顶级视图。 Backbone gives you a lot of flexibility (ie rope to hang yourself with), so you can do things in many ways. Backbone为您提供了很大的灵活性(即绳索可以自行悬挂),因此您可以通过多种方式执行操作。 Generally speaking, however you do it, it works best if you actually construct and append the components of your view before attaching it to the DOM wheverer possible. 一般来说,无论如何,如果您实际构建并附加视图的组件,然后将其更多地附加到DOM,则效果最佳。 This is mentioned in your second example, and in the Backbone docs themselves: 这在第二个示例中提到,在Backbone文档中也是如此:

All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not. 所有视图都始终具有DOM元素(el属性),无论它们是否已插入到页面中。 In this fashion, views can be rendered at any time, and inserted into the DOM all at once, in order to get high-performance UI rendering with as few reflows and repaints as possible. 通过这种方式,可以随时呈现视图,并一次性将所有视图插入到DOM中,以便通过尽可能少的回流和重绘来获得高性能的UI呈现。

Projects like Marionette implement a lot of Backbone best practices and let you operate at a higher level of abstraction. Marionette这样的项目实现了许多Backbone最佳实践,让您可以在更高的抽象级别上运行。 I highly recommend checking it out, instead of trying to figure out all the best-practice patterns yourself. 我强烈建议您查看,而不是试图找出所有最好的练习模式。

It's kinda both... el represents the dom element which your view will be rendered into. 它有点...... el表示你的视图将被渲染成的dom元素。 The template for your view probably has multiple top-level elements. 视图的模板可能包含多个顶级元素。 To simplify things, when backbone renders a view, it uses a "parent" element to wrap the contents of your template. 为了简化操作,当主干呈现视图时,它使用“父”元素来包装模板的内容。 By default this element is a div (set tagName to override this and/or set className to set css classes on it) and is what el refers to. 默认情况下,此元素是div(设置tagName以覆盖此设置和/或设置className以在其上设置css类),并且是el所指的。

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

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