简体   繁体   中英

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.

Well, when they argue that it is very event driven, they pit it against 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.

My argument is that may be Jquery needs explicit management of code to achieve this but event driven programming is not something exclusive? 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?

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, uses events (yes with more code in certain instances) but it doesn't specify a particular pattern to be used. 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.

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. But that will require bit more effort as jQuery is not made to specifically work as such. 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.

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. But for complex UI with high bidirectional messaging, knockout will do things with lesser amount of code.

Take other scenarios as well... Knockout needs you to tamper the HTML with it's custom attributes. What if changing HTML is not an option for you? jQuery has a better use case there. Use jQuery, Knockout, Backbone or BoilerplateJS we per the use case.. not just because someone think it's cool :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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