视图与Ember.js中的组件 [英] Views vs Components in Ember.js

查看:84
本文介绍了视图与Ember.js中的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习ember.js,我正在努力了解视图和组件之间的区别。我看到这两种方式都是可重复使用的组件。



从Ember的网站上查看:


Ember.js中的视图通常仅由于以下原因而创建:
- 当您需要复杂的用户事件处理时
- 当您要创建一个可重复使用的组件


从Ember网站上的组件:


组件是一个自定义HTML标签,其行为使用JavaScript实现,您的外观使用Handlebars模板描述。它们允许您创建可以简化应用程序模板的可重用控件


那么主要区别是什么?在视图和组件之间?那么我会喜欢使用一个组件的视图,反之亦然呢?

解决方案

Ember.View



An Ember.View 目前仅限于W3C为您创建的标签。但是,如果您想定义自己的应用程序特定的HTML标签,然后使用JavaScript实现他们的行为?您不能使用 Ember.View 实现此功能。



Ember.Component



这正是组件让你做的。事实上,W3C目前正在制作自定义元素规范



Ember的组件实现尽可能地与Web Components规范尽可能的一样。 自定义元素 广泛使用您可以轻松地将Ember组件迁移到W3C标准,并可以通过其他框架使用它们,并采用新标准。


这对我们来说非常重要,我们正在与标准机构密切合作,以确保我们的组件实现与Web平台的路线图相匹配。


同样重要的是, Ember.Component 实际上是 Ember.View (一个子类),但完全是隔离。其模板中的属性访问转到视图对象,并且操作也以视图对象为目标。所有上下文信息都被传递到 c c>或外部控制器 ,实际上可以访问其周边控制器的 Ember.View 不是这样的在视图中,您可以执行类似 this.get('controller')的操作,这将使您当前与视图关联的控制器。


那么视图和组件之间的主要区别是什么?


所以,除了这些组件之外,主要的区别可以让你创建自己的标签,并且在某些时候,当 自定义元素 也可以迁移/使用将支持自定义元素的其他框架中的这些组件,确实在某种程度上根据具体的实现情况,一个余烬组件会使视图有些过时。


那么我会喜欢使用对组件的视图,反之亦然?


以上内容取决于您的用例。但作为经验法则,如果您需要在其视图中访问其周围的控制器等,请使用 Ember.View ,但是如果要隔离视图并仅传递其需要工作的信息,使其与上下文无关并且可重用,请使用 Ember.Component



希望它有帮助。



更新



随着 Road to Ember 2.0 在大多数情况下,现在鼓励您使用组件而不是视图。


I am learning ember.js, and I am trying to understand the difference between a view and a component. I see both as a way of making reusable components.

From Ember's website on views:

Views in Ember.js are typically only created for the following reasons:
-When you need sophisticated handling of user events
-When you want to create a re-usable component

From Ember's website on components:

A component is a custom HTML tag whose behavior you implement using JavaScript and whose appearance you describe using Handlebars templates. They allow you to create reusable controls that can simplify your application's templates.

So what is the main difference between a view and a component? And what would be a common example where I would prefer to use a view over a component and vice versa?

解决方案

Ember.View

An Ember.View is currently limited to the tags that are created for you by the W3C. But if you wanted to define your own application-specific HTML tags and then implement their behavior using JavaScript? You can't do this actually with a Ember.View.

Ember.Component

That's exactly what components let you do. In fact, it's such a good idea that the W3C is currently working on the Custom Elements spec.

Ember's implementation of components tries to be as closely to the Web Components specification as possible. Once Custom Elements are widely available in browsers, you should be able to easily migrate your Ember components to the W3C standard and have them be usable by other frameworks as well that have adopted the new standard.

This is so important to us that we are working closely with the standards bodies to ensure our implementation of components matches the roadmap of the web platform.

Also important to note is that a Ember.Component is actually a Ember.View (a subclass) but that is completely isolated. Property access in its templates go to the view object and actions are targeted also at the view object. There is no access to the surrounding context or outer controller all contextual information is passed in, which is not the case with a Ember.View which indeed has access to it's surrounding controller, for example inside a view you could do something like this.get('controller') which would give you the controller currently associated with the view.

So what is the main difference between a view and a component?

So, the main difference besides that components let you create your own tags and in some point in the future when Custom Elements are available also migrate/use those components in other frameworks that will support custom elements, is indeed that at some point an ember component will make a view somewhat obsolete depending on the specific implementation case.

And what would be a common example where I would prefer to use a view over a component and vice versa?

Following the above this depends clearly on your use cases. But as a rule of thumb, if you need in your view access to it's surrounding controller etc. use a Ember.View, but if you want to isolated the view and pass in only the information that it needs to work making it context-agnostic and much more reusable, use a Ember.Component.

Hope it helps.

Update

With the publication of Road to Ember 2.0 you are now encouraged to use Components instead of Views in most of the cases.

这篇关于视图与Ember.js中的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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