Last week I participated in a series of lightning talks at the AngularJS DC Meetup, hosted by Difference Engine, and I thought I'd share the links to the slide decks and demos presented (unfortunately, the equipment recording the entire event failed, otherwise I would just share that).
Not all of the presenters have posted their material, but here what's been shared so far:
Meredith Myers, mostly on a whim, created a cool Angular app that applies API data about rat sightings in New York City to a mapping library called Leaflet. From her description, it sounds like Leaflet is super-easy to use. Her presentation can be found at http://meredithmmyers.com/ratmap-presentation/#/ and her application (with a link to the GitHub repo) can be found at http://meredithmmyers.com/ratmap/#/.
Carl Gieringer gave a very interesting talk about leveraging some of the functionality in the $resource service to perform data transformations and attach behaviors to the objects retrieved via REST. As someone who's used $resource to retrieve a set of data objects and then tossed those objects over to a different service function to do things like transform string dates into JavaScript dates, studying his technique is now part of my to-do list. The presentation, which ends with a link to a GitHub project illustrating part of the technique, is available at http://www.slideshare.net/carlgieringer/object-oriented-ngresource.
Evan Johnson of Andela, who co-sponsored the evening, gave a talk about Andela's effort to train intelligent young men and women in Nigeria to be web and mobile application developers, and their program that matches US programmers as mentors to these students. Their presentation is at http://www.slideshare.net/evanrjohnson/andela-angularjs-dc-meetup.
Louis Wilbrink presented an introduction to Angular Material, the combination of AngularJS with Google's Material Design language for Android and the web. He also created a demo app that he uses to keep track of his contracting hours at http://contract-timer.louiswilbrink.com/#/.
Finally, I gave a ten-minute presentation on $http interceptors in AngularJS: event functions triggered during the request and response phase of HTTP calls made by the built-in Angular services. You can find that at http://www.slideshare.net/bcswartz/angularjs-http-interceptors-explanation-and-examples (I wrote the slides such that they're fairly self-explanatory on their own).
One of the most common uses of the Grunt task runner is to build a deployment package out of your development code for your website or web application, and part of that build process is usually a task that concatenates the CSS and JavaScript files into singular (or at least fewer) files for optimal download.
The grunt-contrib-concat Grunt plugin allows you to configure a concatenation task to target individual files or entire directories, like so:
The only drawback is that you have to update the task's "src" property as you add or remove CSS and JavaScript assets from your web application.
As I was playing around with Grunt on a personal project, I came to wonder: could I create a Grunt task or set of tasks that could figure out which files to concatenate based on the <link> and <script> tags in my code? Here's what I came up with.
My project is a single page web application powered by AngularJS. It has an index.html file that serves as the "single page" that displays the appropriate view fragment (HTML files stored in a "views" directory) for a given page/route in the application. None of those view fragments require additional CSS or JavaScript resources, so my index.html page pulls down all of the necessary CSS and JavaScript files.
In my index.html file, I wrapped my blocks of <link> and <script> tags with special HTML "build" comments, a set for my CSS block and a set for my JavaScript block:
I then created a Grunt task powered by the grunt-replace plugin that finds and parses those blocks via regex, extracting and modifying the file path within each "src" and "href" attribute and appending them to arrays stored as Grunt configuration properities. Each block is replaced by a single <link> and <script> tag pointed at the combined CSS and JavaScript file. The overall build task then executes the concat task, which concatenates all of the files in the respective configuration arrays into the combined files.
So when I run the master build task against my original set of development files:
...I end up with a build directory containing the index.html file, the views folders with the view HTML files, and a single CSS and JavaScript file:
...and the index.html file itself looks like this:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>My HTML Page</title>
<link rel="stylesheet" media="screen" href="combined.css"/>
</head>
<body ng-app="demoApp">
<!--Div where my Angular views will be displayed-->
<div ng-view></div>
<script type="text/javascript" src="combined.js"></script>
</body>
</html>
Cool, right?
If you want to try it out for yourself, the entire Gruntfile used to power the build task is below, with comments:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters