4 ms·
knockout is such an awesome library. I have only used it for a few toy projects, but I love how lightweight it feels; and how it doesnt feel like it takes over
by mattchamb 13y ago
knockout is such an awesome library. I have only used it for a few toy projects, but I love how lightweight it feels; and how it doesnt feel like it takes over all of your html.
I also love how the ko.observable doesnt feel as magical as the angular binding. It makes me feel alot more in control
- mattmanser 13y agoYeah, instead it takes over your javascript turning properties into methods: var myViewModel = { personName: ko.observable('Bob'), personAge: ko.observable(123) }; myViewModel.personName() //get myViewModel.personName('Mary') //set Makes my eyes bleed.
- gdrulia 13y agoYeah, at least this way you have control over which properties are methods. Also, if you really prefer angular.js style of tracking dependencies, this plugin makes the trick http://blog.stevensanderson.com/2013/05/20/knockout-es5-a-plugin-to-simplify-your-syntax/ http://blog.stevensanderson.com/2013/05/20/knockout-es5-a-pl...
- mattmanser 13y agoThanks, looks good. I know it sounds pedantic but it's not 'prefer', it's that the properties as methods is fundamentally changing the way the language works and is very brittle.
- klibertp 13y agohttp://en.wikipedia.org/wiki/Uniform_access_principle http://en.wikipedia.org/wiki/Uniform_access_principle In short, it doesn't matter which it is - a method or an attribute - as long as it's consistent. What fundamental changes to the language you see when using methods instead of plain attributes? Aside from the fact that setter and getter methods in JS are bindable while attribute access is not.
- mattmanser 13y agoYou're kinda arguing against yourself as the code won't be consistent. You're changing a part of your code but not all of it and it just leads to brittle code as you're never sure what you're working with, especially in a dynamically typed language like javascript with terrible support for editor behaviour sniffing. Every time you have an object you have to remember whether it's a normal JS object or a properties-as-methods object. Using knockout to handle your view models doesn't magically change the rest of your code. All the other code you write and libraries you use will be using properties the standard way. Secondly, the uniform access principle (which you seem to misquoting? It's not about consistent access by type, it's about all types having identical methods of access). It comes across as something an academic would propose because it's intellectually enticing but practically detrimental. Think of it like changing a, it, the, we, you, our, etc. all to be o. Yeah, now you can change nouns freely, but the syntax is a signal and the difference is useful. A method is conceptually supposed to be different to a property and so it should look different. Javascript's bad enough for readability with everything being a function. Whether you agree with it or not, in the end it's simply not how javascript works. Methods and properties are accessed differently.
- noir_lord 13y agoThis is amazingly good and very elegant.
- taude 13y agoI hated that at first, but it's really hardly a problem when it comes to actual development. And it's pretty nice for optimization to be able to mix/control what properties on a model actually need the notification changes, as opposed to other frameworks that just assume everything notifies. (Especially useful when you start dealing with object graphs with 100s of properties).
- skilesare 13y agoAngular was supposed to fix this, but when I got down to brass tax I had a hell of a time remembering if I was in or out of context and then having to cast my self back into context. At least with knockout you know you are always out of context and have to use the getters/setters.