4 ms·
It's about imperative vs declarative styles in DOM modification, animations, etc. I've been learning Angular the past few weekends, and I've come across tons of
by farnsworth 13y ago
It's about imperative vs declarative styles in DOM modification, animations, etc. I've been learning Angular the past few weekends, and I've come across tons of SO questions that boil down to "how do I use my familiar jQuery patterns inside Angular?" where the answers are usually "you don't, here's a better way" usually with some Angular directive which fits that pattern naturally. Also, lots of blog posts contrasting the two styles.
As I've gotten comfortable with it, I've come around to seeing it their way. I'll never write jQuery-selector spaghetti code again if I can avoid it. Here's my favorite SO question on the topic: http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background http://stackoverflow.com/questions/14994391/how-do-i-think-i...
- lerouxb 13y agoI hear you and I don't disagree with you. I just freak out when I open a console and $ doesn't exist :)
- pablobaz 13y agoFirebug has a $ built in just for debugging.
- ssmoot 13y ago> It's about imperative vs declarative styles in DOM modification, animations, etc. I don't understand. I mean, Angular doesn't seem any more "declarative" than the Behavior.js you could write 7 or 8 years ago. And jQuery made setup a bit different, but you could easily still keep doing the same thing with declarative decoration. When talking about directives (specifically) Angular doesn't appear (to me) to really offer anything new outside of some questionable conventions for structure.
- bmm6o 13y agoAngular is an MVC framework, which means that your code should be updating the model and letting Angular push that to the view. A lot of the stuff you would do with jQuery is directly updating the view. So instead of writing something like: $('#whatever').addClass('selected'); // say this is in an event handler you should write: $scope.isSelected = true; // this is your ng event handler and the html is like this <div ng-class="{selected: isSelected}"> That is, you think a lot more about the model and how it's represented in the view (declarative) and less about what to do to the view to make it look the way you want (imperative). That's not to say that there's no place for jQuery in an angular app, but it does replace a lot of the typical uses. And fighting it just leads to frustration.
- ssmoot 13y agoI guess it's a grey area, but that all looks pretty imperative to me. I think a Calendar Directive is probably a better example. But the declarative part (ng-calendar vs a jQuery binding for data-calendar) is the smallest part of the effort. I don't know... it all just feels like a lot of work for little benefit. Back in the Rails 0.x to 1.x days even point releases routinely broke applications. In hindsight it was a terrible idea to build on the platform at that time. Going with more mature solutions would ultimately be more cost effective for anything but prototype level demonstrations (IME). Angular feels a lot like that. Plus the whole nested controllers are pretty reminiscent of Windows Forms programming. Which was an awful paradigm that was replaced with stuff much closer to HTML for a reason. So Angular feels like repeating the mistakes of the past. On top of that you're using a fairly terrible language, and reintroducing the idea that you need to write tests to prove syntax and correctness at a text level. Then you write more code to accomplish less. And 6 months on you've spent more money than the team who went with HTML and jQuery, and over the long term you've increased maintenance costs over that team's output if they've put any care at all into maintainability, encapsulation, modularization, etc. That's just my personal experience anyways. It's an awful, terrible, complex, fragile framework with a lot of busy-work attached in a runtime that's just not very good with these patterns. I mean Patterns are Typed. That's the unspoken bit. You can't describe an ActiveRecord, a DataMapper, MVC, MVP, Strategy, etc without Types. And JS has about the weakest Type system among mainstream languages I can think of. I implemented a "JavaScript on Rails" sort of system in JScript in ASP Classic some 8 or so years ago. The fact that the runtime is persistent now and parsers much faster doesn't much change the fact that it's still an awful idea IME leading to... well, a lot of wasted time and money when you could instead be doing something predictable, that's more maintainable, runs faster, and is less expensive to develop and maintain. But maybe that's just me. :)
- bmm6o 13y agoSure, I wish Javascript had a stronger type system too, but I'm not sure how that's relevant to an Angular/jQuery discussion. You still have business logic that you either test or don't test, right? At least angular globally supports DI for sane unit testing. You're right that directives are very declarative, but I didn't mention them because they don't really have a counterpart in jQuery unless you rolled your own. I like Angular a lot. Even just the data binding and $watch system would relieve a lot of tedious code and can make for a really responsive UI.