The Ajax Aspect
Ajax is important to much of what I do in my projects. Headers and JSON get sent back and forth a lot. Also, there are plenty of callbacks for dealing with stuff from the server.
So how do Angular and jQuery compare in that department? To be honest, you don't lose much. You go without Angular's $http service but you still have jQuery's ajax function to work with. Here's what a json-in-json-out scenario looks like in AngularJS.
Here's the same thing but done with jQuery.
Overall, the only significant feature here is that $http has to be dependency injected. I don't know that I gain anything from grabbing a service from out of whatever container it happens to comes from. It feels vaguely like working with a Spring container in an enterprise Java project. But that's about it for me.
So how do Angular and jQuery compare in that department? To be honest, you don't lose much. You go without Angular's $http service but you still have jQuery's ajax function to work with. Here's what a json-in-json-out scenario looks like in AngularJS.
Here's the same thing but done with jQuery.
Overall, the only significant feature here is that $http has to be dependency injected. I don't know that I gain anything from grabbing a service from out of whatever container it happens to comes from. It feels vaguely like working with a Spring container in an enterprise Java project. But that's about it for me.
Know Thy DOM
Giving up Angular did force me to think more about the DOM. Vanilla jQuery doesn't insert itself as readily into HTML code as an Angular directive can. That fact made direct DOM dealings more important. For examples, here's how you generate bullet point list in Angular.
Doing the same thing in jQuery, you get something that looks like this.
While trivial in this example, string concatenation can gets messy as the HTML gets more involved. However, you do get that upshot in jQuery of having debugger features more conveniently available. Setting a breakpoint in straight Javascript is easy. I have no idea how to do that with an ng-repeat loop.
Filling The Template Void with Handlebars.js
The good news is that living without Angular doesn't mean giving up on client-side templates entirely. It turns out that Handlebars.js fills that void nicely. Drop a template between script tags. Load it up. Insert the data. All is well.
Yes, there is DOM dirty work to do but that's okay. At least there isn't that error-prone process of piecing strings together so much. That's actually pretty nice.
Yes, there is DOM dirty work to do but that's okay. At least there isn't that error-prone process of piecing strings together so much. That's actually pretty nice.
My Dynamic Button Trip Up
One area that tripped me up with Handlebars was button handling. Buttons for deleting and changing notes are specific to each note. The event handlers for those buttons need to reflect that.
Handlebars is really good at taking data text and plugging it into template text. Where it gets weird is when events come into the picture.
The thing is this. You generally don't attach event handlers directly to the text that Handlebars generates. You attach them to some kind of DOM object like this...
Okay, so what to do?
Here was my solution. Let Handlebars do the whole template thing. Have jQuery wrap the text up as a DOM element. Then, just let jQuery attach event handlers to that. Here is what that code ended up looking like.
Handlebars is really good at taking data text and plugging it into template text. Where it gets weird is when events come into the picture.
The thing is this. You generally don't attach event handlers directly to the text that Handlebars generates. You attach them to some kind of DOM object like this...
$("#myButton").click(function(e){})
Here was my solution. Let Handlebars do the whole template thing. Have jQuery wrap the text up as a DOM element. Then, just let jQuery attach event handlers to that. Here is what that code ended up looking like.
So that gets the job done. Once the DOM is created, we can attach a DOM button to that structure. And that's it. In case you're curious, you can find the full js file where this is set up over here. And if I'm "doing it wrong", feel free to yell at me in the comments section.
Final Thoughts
Going without Angular was nowhere near as hard as I thought it would be. The Ajax parts are reasonable thanks to the core jQuery libraries. The DOM aspects I found myself needing to handle really weren't that burdensome. And thanks to Handlebars, I didn't even have to give up templates.
What Angular is good for is catering to my enterprise and Java background. I've done my share of mixing logic and HTML together from my experiences in JSP and ColdFusion. The dependency injection that happens in Angular feels like a simplified version of what happens in a lot of Spring projects.
All being said, I don't think I'm going to disavow AngularJS entirely because I'm still not convinced that it's a "bad" framework. For bigger web applications, it might well be ideal for all I know. It's just that I now know that the set of problems Angular fits for isn't as big as I thought. And that's an okay thing to come to terms with. Here's to the programmer's journey!
What Angular is good for is catering to my enterprise and Java background. I've done my share of mixing logic and HTML together from my experiences in JSP and ColdFusion. The dependency injection that happens in Angular feels like a simplified version of what happens in a lot of Spring projects.
All being said, I don't think I'm going to disavow AngularJS entirely because I'm still not convinced that it's a "bad" framework. For bigger web applications, it might well be ideal for all I know. It's just that I now know that the set of problems Angular fits for isn't as big as I thought. And that's an okay thing to come to terms with. Here's to the programmer's journey!