简体   繁体   English

Knockout完全由事件驱动吗?

[英]Is Knockout exclusively event driven?

Well, I am basically exploring knockout and and understanding different features that it has to offer. 好吧,我基本上是在探索淘汰赛,并了解它所提供的不同功能。 I really liked the templates and two way binding with some observation with it. 我真的很喜欢模板和双向绑定与它的一些观察。 Now, it is implemented in our company recently for may modules but there is a really big talk about how event driven it is and how beautiful it is. 现在,它已经在我们公司最近为may模块实现了,但是关于事件驱动它是如何以及它是多么美丽有一个非常大的讨论。

Well, when they argue that it is very event driven, they pit it against JQuery. 好吧,当他们认为它是由事件驱动时,他们会反对JQuery。 Their argument is that in JQuery anyone can come in and bind a button's onClick anywhere and it gets messy sometimes when there is no set standard for keeping all the events for all the controls in the same place and it makes code hard to manage. 他们的论点是,在JQuery中,任何人都可以进入并在任何地方绑定按钮onClick,当有没有设置标准来保持所有控件在同一个地方的所有事件并且它使代码难以管理时,它会变得混乱。

My argument is that may be Jquery needs explicit management of code to achieve this but event driven programming is not something exclusive? 我的论点是,Jquery可能需要对代码进行显式管理才能实现这一目标,但事件驱动编程并不是排他性的? asp.net web pages have it with it's code behind for years and how about classic ASP with ancient JavaScript with all it's OnClick events? asp.net网页拥有它的代码背后多年以及如何使用古老的JavaScript及其所有OnClick事件的经典ASP?

Am I missing anything here? 我在这里错过了什么吗?

Well events are not new.. Since the inception of a button control, it should have been there in one way or another. 好事件不是新的..自从按钮控件开始以来,它应该以某种方式存在。 But the difference is how event are being used and how maintainable your can be. 但不同之处在于如何使用事件以及您可以如何维护。

On jQuery and Knockout, I dont think it is a question of events, but a question of the design pattern. 在jQuery和Knockout上,我不认为这是事件的问题,而是设计模式的问题。 jQuery, uses events (yes with more code in certain instances) but it doesn't specify a particular pattern to be used. jQuery,使用事件(在某些情况下是更多代码)但它没有指定要使用的特定模式。 On the other hand KnockoutJS brings in MVVM pattern with bi-directional data binding, that will give you a structure required for writing complex applications. 另一方面,KnockoutJS引入了具有双向数据绑定的MVVM模式,这将为您提供编写复杂应用程序所需的结构。

In my opinion, use of a pattern will certainly enhance your code maintainability. 在我看来,使用模式肯定会增强代码的可维护性。 You may even implement MVVM, MVC or MVP with plain jQuery too. 您甚至可以使用普通的jQuery实现MVVM,MVC或MVP。 But that will require bit more effort as jQuery is not made to specifically work as such. 但这需要更多的努力,因为jQuery没有专门用于此类工作。 For example if you use BackboneJS that will let you to organize your code on MVC pattern and there you may use jQuery for event bindings. 例如,如果您使用BackboneJS,它将允许您在MVC模式上组织代码,那么您可以使用jQuery进行事件绑定。

Each of the library has it's own place depending on the use case. 根据用例,每个库都有自己的位置。 For simple one way event handling, you might not need knockout viewmodels but just do that clean with jQuery. 对于简单的单向事件处理,您可能不需要淘汰视图模型,但只需使用jQuery清理它。 But for complex UI with high bidirectional messaging, knockout will do things with lesser amount of code. 但对于具有高双向消息传递的复杂UI,淘汰赛将使用较少量的代码执行操作。

Take other scenarios as well... Knockout needs you to tamper the HTML with it's custom attributes. 同样采取其他方案...... Knockout需要您使用自定义属性来篡改HTML。 What if changing HTML is not an option for you? 如果更改HTML不适合您,该怎么办? jQuery has a better use case there. jQuery有一个更好的用例。 Use jQuery, Knockout, Backbone or BoilerplateJS we per the use case.. not just because someone think it's cool :) 根据用例使用jQuery,Knockout,Backbone或BoilerplateJS ..不仅仅是因为有人认为它很酷:)

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

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