简体   繁体   English

我何时应该使用KnockoutJS组件与模板?

[英]When should I use KnockoutJS Components vs. Templates?

So trying to get my hands dirty with KnockoutJS 3.2. 所以试着用KnockoutJS 3.2弄脏我的手。 I've read the docs and I've successfully implemented components within my current project. 我已经阅读了文档,并且我已经在当前项目中成功实现了组件。 I don't use an AMD, so I'm just using script elements to hold the views. 我不使用AMD,所以我只是使用脚本元素来保存视图。

My question is: If i'm not using the asynchronous loading features, is there any real practical difference to using components rather than templates? 我的问题是:如果我没有使用异步加载功能,使用组件而不是模板是否有任何实际的区别?

General description of both 两者的一般描述

As stated in the other answer, a template is only a piece of HTML which can be bound to a viewmodel, or viewmodel section. 如另一个答案所述,模板只是一段HTML,可以绑定到viewmodel或viewmodel部分。 And a component is composed of a template, and its corresponding viewmodel. 并且组件由模板及其对应的视图模型组成。 Besides, this viewmodel, apart from observables can include some simple business logic, and functionality to communicate with the server. 此外,除了observable之外,这个视图模型可以包括一些简单的业务逻辑和与服务器通信的功能。

Coupling and encapsulation 耦合和封装

Another important difference is the coupling. 另一个重要的区别是耦合。 A template it's bound to the main viewmodel, and its bound to the main viewmodel's observables, so it's highly coupled to the viewmodel: a change in the viewmodel will break the template, and viceversa. 一个模板,它绑定到主视图模型,并且绑定到主视图模型的可观察对象,因此它与视图模型高度耦合:视图模型中的更改将破坏模板,反之亦然。 So, if you're reusing a template in several places, and you change it, you have to correct the corresponding viewmodels. 因此,如果您在多个位置重复使用模板,并且更改了它,则必须更正相应的视图模型。

A component is bound to its own viewmodel. 组件绑定到自己的viewmodel。 It's only coupled to the main viewmodel if there are parameters provided from it. 如果有从它提供的参数,它只耦合到主视图模型。 This means that you can easily change the component template as well as the component viewmodel, and, if there are no parameters, or you don't change them, nothing will be broken. 这意味着您可以轻松更改组件模板以及组件视图模型,如果没有参数,或者您没有更改它们,则不会破坏任何内容。

So using components helps in decoupling and modularizing. 因此,使用组件有助于解耦和模块化。

Communication between main viewmodel and component 主视图模型和组件之间的通信

The last section is a double edged sword: if there is a high interaction between the main viewmodel and the template or component, it's much easier to use a template, because all the logic and properties are held in the main viewmodel and the interactions are easily implemented. 最后一部分是一把双刃剑:如果主视图模型与模板或组件之间存在高度交互,则使用模板要容易得多,因为所有逻辑和属性都保存在主视图模型中,并且交互很容易实现。 If you used a component you'd need to provide complex parameters, or even make something to allow the component to expose functionality to the main viewmodel. 如果您使用了组件,则需要提供复杂的参数,甚至可以创建一些允许组件向主视图模型公开功能的内容。

Polymorphism 多态性

It's not strange to have some parts of an application that require different behaviors and visualization to solve the same kind of task. 让应用程序的某些部分需要不同的行为和可视化来解决相同类型的任务并不奇怪。 For example, let's imagine you have to implement a payment system in your application: if you accept for example paypal and credit card payment you have two different visualization and functionalities. 例如,让我们假设你必须在你的应用程序中实现一个支付系统:如果你接受例如paypal和信用卡支付,你有两种不同的可视化和功能。 If you used templates, you'd need to have the particular implementation of each payment system in the main viewmodel. 如果您使用模板,则需要在主视图模型中具有每个支付系统的特定实现。 If you used components, they'd share a common interface (parameters) but each of them would have its own implementation. 如果您使用了组件,它们将共享一个公共接口(参数),但每个组件都有自己的实现。 If tomorrow you had to include a new payment system, it would be easy to implement a new component with the common interface. 如果明天你必须包含一个新的支付系统,那么使用通用接口实现一个新组件会很容易。

NOTE: pay attention to the last paragraph 注意:注意最后一段

Binding 捆绑

In the case of a template the binding it's not done at the template level, but inside it. 在模板的情况下,绑定不是在模板级别,而是在模板内部。 Ie each element inside the template must be bound to observables of the main viewmodel. 即模板中的每个元素必须绑定到主视图模型的可观察对象。 In the case of a component the binding is much simpler: at most, it requires the component name and the parameters, if they exist. 对于组件,绑定更简单:最多,它需要组件名称和参数(如果存在)。

Component registration and custom tags 组件注册和自定义标签

If you register the component you can use custom tags. 如果您注册该组件,您可以使用自定义标签。 This makes the views more easyly readable and understandable: instead of specifying the component name in a binding, you use a tag with the component name, and parameters are passed as attributes. 这使得视图更易于阅读和理解:不是在绑定中指定组件名称,而是使用带有组件名称的标记,并将参数作为属性传递。

Dynamic loading 动态加载

If you use templates you have to dynamically load them by yourself, and, as this is an asynchronous task, you'll have to take care of using the template only when it's already available. 如果您使用模板,则必须自己动态加载它们,并且由于这是一个异步任务,因此只有在模板已经可用时才需要使用该模板。 That's way in most occasions you'll use inline templates. 在大多数情况下,您将使用内联模板。 This is not good if they must be reused in several places. 如果它们必须在几个地方重复使用,那就不好了。

If you have used some AMD implementation, like require.js, and you understand the benefits of this technology, you'll be happy to know that you can easily use AMD to load component templates and viewmodels. 如果你使用了一些AMD实现,比如require.js,并且你了解这项技术的好处,你会很高兴知道你可以轻松地使用AMD加载组件模板和视图模型。 One of the advantages is that you don't have to worry about the template or component being available when you need to use them. 其中一个优点是,您不必担心在需要使用模板或组件时可用。

Testability 可测性

No matter if you do manual or automated tests, it's much easier to test a bunch of independent components, one by one, that to test a complex viewmodel with or without templates. 无论您是进行手动还是自动化测试,都可以更轻松地逐个测试一堆独立组件,以测试带有或不带模板的复杂视图模型。

My choice 我的选择

So far, I've exposed facts about templates, and components, and I've tried not to show my personal preferences. 到目前为止,我已经公开了有关模板和组件的事实,我试图不显示我的个人偏好。 However, in this last section, I must say that in most situations I prefer to use components for their advantages: 但是,在最后一节中,我必须说在大多数情况下我更喜欢使用组件来获得它们的优势:

  • modularity 模块化
  • low coupling 低耦合
  • easy reuse 易于重用
  • testability 可测试性
  • binding syntax 绑定语法
  • (optional) dynamic loading (可选)动态加载

However, templates are also a better fit on some occasions 但是,模板在某些情况下也更适合

The last paragraph have to do with big applications. 最后一段与大型应用程序有关。 If you're dealing with small applications or simply enhancements of interfaces rendered by another technology (like ASP.NET MVC), you'll probably get none of the advantages of using components. 如果您正在处理小型应用程序或仅处理由其他技术(如ASP.NET MVC)呈现的界面的增强,您可能无法获得使用组件的任何优势。 So, you don't need them. 所以,你不需要它们。

There are other cases when it's not worth using components. 在其他情况下,不值得使用组件。 For example, if you have to show a list (JavaScript array) of items which have different properties which must be shown in a different way, it's easier to use templates. 例如,如果必须显示具有不同属性的项目列表(JavaScript数组),这些项目必须以不同方式显示,则使用模板会更容易。 Note that the choice in this case is because each of the instances doesn't have a complex viewmodel with a lot of functionality, but a simple bunch of properties. 请注意,在这种情况下的选择是因为每个实例都没有具有许多功能的复杂视图模型,而是一组简单的属性。 In this particular case it's not only not worth, but it can also be counterproductive to use components. 在这种特殊情况下,它不仅不值得,而且使用组件也会适得其反。

You can understand this last example as polymorphism. 您可以将此最后一个示例理解为多态。 But, in this case it's nearly "visual" polymorphism. 但是,在这种情况下,它几乎是“视觉”多态。 Ie each kind of item must be shown in a different way, but there is no need to implement any special logic in each of the components. 即每种项目必须以不同的方式显示,但不需要在每个组件中实现任何特殊逻辑。

However , even in this case, if the templates are complex enough, or must be used in many different places, it's also much better to use a simple component that includes the template as well as a parameter that receives the whole item. 但是 ,即使在这种情况下,如果模板足够复杂,或者必须在许多不同的地方使用,那么使用包含模板的简单组件以及接收整个项目的参数也会好得多。 So, even in this case, it's not a bad idea to use components. 因此,即使在这种情况下,使用组件也不是一个坏主意。

If you'd read this far, thank you, and I hope you have some criteria to help you choosing the best option. 如果你读到这里,谢谢你,我希望你有一些标准来帮助你选择最好的选择。

They aren't completely different. 它们并非完全不同。 Components are made up of templates (html) and data/logic (view model ie JavaScript). 组件由模板(html)和数据/逻辑(视图模型,即JavaScript)组成。 When you have a modular view you want to attach a view model to you can utilize components. 当您有模块化视图时,您想要附加视图模型,您可以使用组件。 Here's a link discussing components a bit more: http://www.knockmeout.net/2014/06/knockout-3-2-preview-components.html 这是一个讨论组件的链接: http//www.knockmeout.net/2014/06/knockout-3-2-preview-components.html

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

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