@rob_rich
by Rob Richardson
October 2, 2013
Rob Richardson is principal of Richardson & Sons, LLC, builder of ASP.NET and Node solutions for small- to medium-sized businesses. Rob has a BFA and MSCIS, and has been building software in .NET and HTML for over a dozen years, and frequently enjoys sharing his passion for software development at user groups and community events. Follow Rob at @rob_rich or on his blog at http://robrich.org/.
Why do we need JavaScript libraries?
Why should we avoid JavaScript libraries?
Use libraries when the download / page-init time
is worth the shorter dev time
Don't use libraries when a few
simple lines could do the same thing
Ready?
An animation, effects, and ajax library
Smooths over inconsistencies in browser DOMs
81.6k
$('#save').click(function () {
var name = $('#name').val();
$.post('/service', {name: name}, function (r) {
$('#status').text('status: '+r.message);
});
});
Object and array tools (think LINQ)
Feature compatible, speed optimized version of underscore
26.1k
var stooges = [
{ name: 'larry', age: 20 },
{ name: 'curly', age: 30 },
{ name: 'moe', age: 40 }
];
var match = _.where(stooges, { age: 40 });
// [{ name: 'moe', age: 40 }]
var names = _.pluck(stooges, 'name');
// ['larry', 'curly', 'moe']
1-way templating
(super-set of Mustache)
71.0k
<script id="entry-template" type="text/template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
var source = $("#entry-template").html();
var template = Handlebars.compile(source);
var context = {title: "My New Post", body: "This is my first post!"}
var html = template(context);
$("#page").html(html);
Browser feature detection, creates HTML5 elements for old-ie
15.0k*
*build custom version with only the feature tests you need
Modernizr.load({
test: (window.JSON && JSON.stringify && JSON.parse),
nope: "//cdnjs.cloudflare.com/ajax/libs/json3/3.2.5/json3.min.js"
});
.incompatible {
display: none;
}
.no-svg .incompatible {
display: block;
}
Easily parse, format, and calculate dates
18.7k
var d = moment('2013-02-04T10:35:24-08:00');
var s = d.format('M/D/YYYY h:mm:ss A');
// 2/4/2013 10:35:24 AM
var a = d.fromNow();
// 1 year ago
Gently normalize CSS inconsistencies between browsers
7.3k
<link rel="stylesheeet" src="normalize.css" />
CSS grid layout, elegantly restyle standard controls
257k
<div class="row">
<div class="col-md-8">two-thirds</div>
<div class="col-md-4">one-third</div>
</div>
Control library built atop jQuery
285k
$("#date").datepicker();
Data validation framework
20.5k
<form id="commentForm">
<input id="name" name="name" minlength="2" type="text" required placeholder="Name *" />
<input id="email" type="email" name="email" required placeholder="Email *" />
<input id="url" type="url" name="url" />
<label for="comment">Comment *</label>
<textarea id="comment" name="comment" required></textarea>
<input class="submit" type="submit" value="Submit"/>
</form>
<script>
$("#commentForm").validate();
</script>
MV*-like framework provides structure and separation
19k
var DocumentRow = Backbone.View.extend({
tagName: "li",
className: "document-row",
events: {
"click .icon": "open",
"click .button.edit": "openEditDialog",
"click .button.delete": "destroy"
},
initialize: function() {
this.listenTo(this.model, "change", this.render);
}
render: function() {
...
}
});
MV*-framework, 2-way data-binding, fake web parts
78.9k
<html ng-app>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="HelloCntl">
Your name: <input type="text" ng-model="name" value="World"/>
Hello {{name}}!
</div>
</body>
</html>
function HelloCntl($scope) {
$scope.name = 'World';
}
Check JavaScript for common errors in IDE or CI
Community-maintained fork of JSLint
295k (not deployed)
JavaScript testing harness
250k (not deployed)
describe("A suite", function() {
it("contains spec with an expectation", function() {
"the answer".should.be("the answer");
});
});
AMD module loading framework
14.7k
define(['require', 'dependency1', 'dependency2'], function (require) {
var dependency1 = require('dependency1'),
dependency2 = require('dependency2');
return function () {};
});
SVG Charting library
89.2k
Library | Size |
---|---|
jQuery | 81.6k |
Lo-Dash | 26.1k |
Handlebars | 71.0k |
Modernizr | 25.0k |
Moment | 18.7k |
Normalize.css | 7.3k |
Twitter Bootstrap | 257k |
jQuery UI | 285k |
jQuery validate | 20.5k |
Backbone | 19k |
Angular | 78.9k |
JSHint | 295k |
Mocha | 250k |
RequireJS | 14.7k |
Rafael | 89.2k |
Total | !!!!!!k |
* To be fair, I've not gzipped these libs
Don't use a library, do it yourself
depends
var name = document.getElementById('name');
name.innerHTML = 'this is the text';