Node Roundup: Surviving npm Downtime, Waf Wall of Shame, stream-chat, Vein

15 Aug 2012 | By Alex Young | Comments | Tags node modules RPC WebSocket npm templating
You can send in your Node projects for review through our contact form or @dailyjs.

Surviving npm Downtime

On Sunday morning (UTC) part of npm’s service stopped functioning correctly, which meant the command-line tool failed to download packages. In response to this, Dominique Sandoz stepped up and created a mirror. Mirrors can be used by changing the location of the npm registry:

npm set registry http://npm.example.com
npm set registry https://registry.npmjs.org

Fortunately npm is now working correctly, but to help prepare for future downtime I recommend bookmarking this gist that Dominique wrote containing a list of npm mirrors and proxies.

During the downtime, several other users also offered their own solutions, mostly collaborating on Twitter and IRC. For example, Maciej Małecki created a proxy-based solution.

To my knowledge there has been no official statement on the service outage, and additionally there is no official Node/npm service status page. Hopefully someone will write up details about the outage on blog.nodejs.org, and outline the preventative measures taken to ensure future stability.

Node-waf Wall of Shame

Isaac Schlueter posted a Tweet referencing what he calls the “Node-waf wall of shame”:

Node-waf wall of shame: http://j.mp/node-waf-users Upgrade your packages to node-gyp. http://npm.im/node-gyp #deathtowaf

As I mentioned in the Windows and Node series, it’s time to move addons over to node-gyp.

stream-chat

stream-chat (License: MIT) by Raynos is a chat app based around streams. It uses the boot and multi-channel-mdm modules by the same author to route streams between browsers and Node servers.

An added bonus is the author has designed the chat app to be scalable by using seaport and mountie by James Halliday. Seaport helps manage clusters of servers, while mountie allows applications to be composed of smaller servers behind mount points. Routing is handled by bouncy. These modules help design larger systems according to the concept of separation of concerns, which suits Node web applications.

MinnaHTML.js

MinnaHTML.js (License: GPL v3, npm: minnahtml) by Robee Shepherd is an async-friendly HTML object library that can be used to build HTML:

var mh = require('minnahtml').mh
  , html = new mh.Html()
  , head = new mh.Head(html)
  ;

 new mh.Title(head).data.content = 'Example';
 
 console.log(html.generateHtml());

// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
// <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
//  <head>
//   <title>
//    Example
//   </title>
//  </head>
// </html>

The author has provided a walkthrough that covers more features in the project’s readme file.

Vein

Vein (License: MIT, npm: vein) is a WebSocket RPC module. It includes both client and server code, and a minimised build for browsers. RPC methods can be added using vein.add, and then called from clients:

// Server
vein.add('multiply', function(res, numOne, numTwo) {
  res.reply(numOne * numTwo);
});

// Browser
var vein = Vein.createClient();
vein.on('ready', function(services) {
  vein.multiply(2, 5, function(num) {
    // num === 10
  });
});

jQuery Roundup: jQuery 1.8, BBSearch, MDMagick, Document-Bootstrap, DownloadBuilder.js

14 Aug 2012 | By Alex Young | Comments | Tags jquery plugins backbone.js sessionStorage files
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

jQuery 1.8

jQuery 1.8 has been released. jQuery 1.8 makes some fairly significant changes:

  • The selector engine has been rewritten
  • Animations have been reworked
  • .css and .animate will automatically add vendor prefixes
  • The size has been reduced compared to jQuery 1.7.2
  • Grunt build system

BBSearch

BBSearch (GitHub: fguillen / BBSearch, License: CC BY 3.0) by Fernando Guillen is a find-as-you-type style plugin that’s designed to work with Backbone.js. It can also work with JSONP APIs, and the author has included demos that use GitHub and Twitter.

$('#search-input-1').bbsearch({
  url: 'https://api.github.com/legacy/repos/search/#bbsearch-query#?callback=?&'
, itemTemplate: '<p>[@<%= owner %>] <a href="<%= url %>"><%= name %></a></p>'
, resultsElement: $('#results-1')
, parse: function(response) { return response.data.repositories; }
});

MDMagick

MDMagick

MDMagick (GitHub: fguillen / MDMagick, License: CC BY 3.0) also by Fernando Guillen is a Markdown interface for jQuery. It uses Showdown, which is a Markdown parser for JavaScript, and a-tools for handling text selections.

Document-Bootstrap and DownloadBuilder.js

Document-Bootstrap (GitHub: gfranko / Document-Bootstrap, License: MIT) by Greg Franko is a boilerplate for creating responsive Bootstrap projects. It also includes DownloadBuilder.js (GitHub: gfranko / DownloadBuilder.js, License: MIT), a project by the same author that uses the HTML5 File API to create optimised client-side scripts.

DownloadBuilder.js supports concatenating local files and files hosted on GitHub. It can also cache Ajax/JSONP requests by using sessionStorage.

DailyJS is Looking for Writers

13 Aug 2012 | By Alex Young | Comments | Tags community news

Over the last three years I’ve written over 700 articles for DailyJS. I’ve written through national holidays, trips to Europe and Asia, and I’ve enjoyed every minute of it! The time has come, however, to look for more writers. In September I’m going to become a father, and naturally family obligations will take priority for a few weeks.

Rather than leave readers without their daily dose of JavaScript news and tutorials, I’m looking for writers to contribute to the site. If you’re an open source developer and would like to promote your latest Node module or client-side library by writing a tutorial: this is your chance! I’m also interested in hearing from developers of commercial services that are relevant to JavaScript developers.

Before writing anything, fill out this form to get in touch. I’ll let you know if your suggestion is appropriate for the site.

Writing Guide

  • Articles do not need to be long – three to five paragraphs is great, otherwise consider writing a series of articles
  • Markdown is fine, although we use weird tags for code samples – here is an example post
  • British or American English is acceptable
  • DailyJS reserves the right to edit your article before publishing it

Payment

Articles will be paid for at a base rate of $25. Payment terms are negotiable, and may be subject to exchange rate conversion.

Submit your article idea here.

HubSearch, canvas-charts, Motown

10 Aug 2012 | By Alex Young | Comments | Tags libraries browser ui windows apps metro

HubSearch

HubSearch

HubSearch (GitHub: jgallen23 / hubsearch) by Greg Allen is a GitHub search interface built with Bootstrap, Underscore, and his own Fidel library for Backbone-style controllers.

I spend a lot of time searching GitHub to find projects to write about, so it’s good to see a tool that tries to improve on GitHub’s inadequate search interface.

canvas-charts

canvas-charts (License: MIT) by Praneet Loke is a graph library that only relies on the HTML5 Canvas API. The library includes a method to check if the Canvas element is supported, and it accepts an object containing options:

var chartingCanvas = new CanvasCharts();
if (chartingCanvas.isCanvasSupported()) {
  chartingCanvas.configure{
    gradientColor1: '#cd9603'
  , gradientColor2: '#f1de3f' // etc.
  });

  chartingCanvas.createCanvas($('graph'), width, height);
  // Given suitable arrays of labels for the x and y axes
  chartingCanvas.drawChart(xaxis, 90, yaxis, 35, [10, 20, 30 20, 10]);
}

Motown

Motown (GitHub: mtilchen / motown, License: LGPL v3) by Matthew P. Tilchen is a library for creating Metro-style Windows 8 applications using HTML, CSS, and JavaScript. Matthew sent this in before Microsoft decided to change the Metro name, but at this point everyone interested in this project should understand the name’s origin.

The WinJS libraries and bindings to WinRT provide a solid foundation for building applications but I feel that a significant gap remains. Microsoft […] intentionally left […] this for the development community to fill. I created the Motown JavaScript library […] to fill the gap.

This library is based on Microsoft’s JavaScript Windows API, and is built according to Microsoft’s recommendations:

Microsoft recommends that developers use a “single-page” style of navigation instead of a “multi-page” style in their applications.

Motown also supports navigation transition animations, application life cycle methods, and a declarative approach to binding HTML views to JavaScript. The project includes a Visual Studio 2012 plugin for creating new Motown apps.

Backbone.js: Hacker's Guide Part 4

09 Aug 2012 | By Alex Young | Comments | Tags mvc tutorials backbone.js code-review

Last week we looked at Backbone’s History and View APIs. We’re coming to the end of this detailed look at Backbone’s internals, but there are still a few interesting things left:

  • Backbone’s inheritance implementation
  • Backbone.sync

Backbone’s inheritance Implementation

The comments indicate that the inherits function is inspired by goog.inherits. Google’s implementation is from the Closure Library, but Backbone’s API accepts two objects (incorrectly referred to as a hash) containing “instance” and “static” methods. Each of Backbone’s objects has an extend method:

Model.extend = Collection.extend = Router.extend = View.extend = extend;

Most development with Backbone is based around inheriting from these objects, and they’re designed to mimic a classical object-oriented implementation.

Backbone uses Underscore’s extend method:

each(slice.call(arguments, 1), function(source) {
  for (var prop in source) {
    obj[prop] = source[prop];
  }
});
return obj;

This isn’t the same as ES5’s Object.create, it’s actually copying properties (methods and values) from one object to another. Since this isn’t enough to support Backbone’s inheritance and class model, the following steps are performed:

  1. The instance methods are checked to see if there’s a constructor property. If so, the class’s constructor is used, otherwise the parent’s constructor is used (i.e., Backbone.Model)
  2. Underscore’s extend method is called to add the parent class’s methods to the new child class
  3. The prototype property of a blank constructor function is assigned with the parent’s prototype, and a new instance of this is set to the child’s prototype property
  4. Underscore’s extend method is called twice to add the static and instance methods to the child class
  5. The child’s prototype’s constructor and a __super__ property are assigned

This pattern is also used for classes in CoffeeScript, so Backbone classes are compatible with CoffeeScript classes.

Update: Jeremy Ashkenas clarified this process on Twitter:

… it’s just your basic prototype chain, plus one extra goodie: any constructor properties (static) are copied over as well.

Backbone’s Sync API

The Backbone.sync method is intended to be overridden to support other backends. The built-in method is tailed to a certain breed of RESTful JSON APIs – Backbone was originally extracted from a Ruby on Rails application, which uses HTTP methods like PUT the same way.

The way this works is the model and collection classes have a sync method that calls Backbone.sync. Both will call this.sync internally when fetching, saving, or deleting items.

The sync method is called with three parameters:

  • method: One of create, update, delete, read
  • model: The Backbone model object
  • options: May include success and error methods

Implementing a new sync method can use the following pattern:

Backbone.sync = function(method, model, options) {
  var requestContent = {}, success, error;

  function success(result) {
    // Handle results from MyAPI
    if (options.success) {
      options.success(result);
    }
  }

  function error(result) {
    // Handle results from MyAPI
    if (options.error) {
      options.error(result);
    }
  }

  options || (options = {});

  switch (method) {
    case 'create':
      requestContent['resource'] = model.toJSON();
      return MyAPI.create(model, success, error);

    case 'update':
      requestContent['resource'] = model.toJSON();
      return MyAPI.update(model, success, error);

    case 'delete':
      return MyAPI.destroy(model, success, error);

    case 'read':
      if (model.attributes[model.idAttribute]) {
        return MyAPI.find(model, success, error);
      } else {
        return MyAPI.findAll(model, success, error);
      }
  }
};

This pattern delegates API calls to a new object, which could be a Backbone-style class that supports events. This can be safely tested separately, and potentially used with libraries other than Backbone.

There are quite a few sync implementations out there:

Node Roundup: Node 0.8.6, Axon, NextFlow

08 Aug 2012 | By Alex Young | Comments | Tags node modules async pubsub
You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.8.6

Node 0.8.6 is out, and this release is the first to include binary distributions for all supported Unix systems.

The 0.6 series has also been updated, with the release of 0.6.21. This release fixes a bug in fs.watch that affected Solaris.

Axon

Axon (License: MIT, npm: axon) by TJ Holowaychuk is a zeromq-inspired message-oriented socket library. It uses the push/pull and publish/subscribe patterns, and features a lightweight wire protocol that supports binary messages.

Axon is JavaScript, so it might work well in situations where a messaging system is desired but additional software installation is not. The API is friendly for Node developers, particularly the EmitterSocket object which behaves like Node’s EventEmitter.

TJ has included information on the protocol and some rough benchmarks.

NextFlow

NextFlow (License: MIT, npm: nextflow) by JP Richardson is a control flow library for Node that has a CoffeeScript-friendly API. Rather than using a chainable API or a list of arguments, NextFlow accepts an object:

next = require('nextflow')

vals = []
x = 0

next flow =
  1: ->
    vals.push(1)
    @next()
  2: ->
    vals.push(2)
    x = Math.random()
    @next(x)
  3: (num) ->
    vals.push(num)
    @next()
  4: ->
    vals.push(4)
    @next()
  5: ->
    console.log vals[0] #is 1
    console.log vals[1] #is 2
    console.log vals[2] #is x
    console.log vals[3] #is 4

The author has included CoffeeScript comparisons with well-known Node control flow libraries.

jQuery Roundup: AutobahnJS, Grid Builder, Best Ampersand

07 Aug 2012 | By Alex Young | Comments | Tags jquery plugins libraries WebSocket ui
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

AutobahnJS

AutobahnJS (GitHub: tavendo / AutobahnJS, License: MIT) from Tavendo GmbH is a client library that implements the The WebSocket Application Messaging Protocol (WAMP). The API is asynchronous and promise-based, so it can use jQuery’s Deferred object or similar implementations. Publish/Subscribe and RPC are both supported by the library.

The RPC API looks like this:

// WAMP server
var wsuri = 'ws://localhost:9000';
ab.connect(wsuri,
  // WAMP session was established
  function(session) {
    // Asynchronous RPC, returns promise object
    session.call('http://example.com/simple/calc#add', 23, 7).then(
      // RPC success callback
      function(res) {
        console.log('got result:', res);
      },

      // RPC error callback
      function(error, desc) {
        console.log('error:', desc);
      }
    );
  },

  // WAMP session is gone
  function(code, reason) {
    console.log(reason);
  }
);

The authors have written tutorials for Publish/Subscribe with AutobahnJS and RPC.

jQuerin Grid Builder

jQuerin Grid Builder (GitHub: kanakiyajay / jQuerin-grid-builder, License: MIT/GPL) by Jay Kanakiya is an interactive tool that generates HTML and CSS for grid-based layouts.

Attributes like class and id can be added to cells, and multiple levels of rows and columns can be created once you get the hang of the interface.

Best Ampersand

Best Ampersand (GitHub: gmurphey / jQuery.Best-Ampersand, License: MIT/GPL) by Garrett Murphey is a simple plugin that wraps ampersands in spans so they can be styled with the best available ampersand. Garrett has written this plugin using a TDD approach, and he’s included unit tests and the build script.

He also sent in Outbound Analytics, which can help track outbound links with Google Analytics.

JS101: Enumeration

06 Aug 2012 | By Alex Young | Comments | Tags js101 tutorials language beginner

In this article we’ll look at JavaScript’s enumeration capabilities. This is partly related to scope, so take a look at JS101: A Brief Lesson on Scope if you haven’t read it yet.

Correctly Using for-in

The for-in statement is intended to be used to enumerate over the properties of an object. Inherited properties are included, and to avoid iterating over them hasOwnProperty should be used.

This example demonstrates using for-in, and shows what happens when someone adds a property to a native prototype:

Object.prototype.bad = undefined;

var name, names;

names = {
  bill: { age: 44 }
, bob: { age: 22 }
, john: { age: 29 }
};

for (var name in names) {
  if (names.hasOwnProperty(name)) {
    console.log(name, names[name].age);
  } else {
    console.log('Inherited:', name);
  }
}

The output will show the inherited property that we don’t want. Even though the property has been set to undefined, it still exists and will be present when enumerated over. Some libraries may add properties this way (even though it’s generally considered bad practice), so it’s wise to use the following pattern:

for (var prop in exampleObject) {
  if (exampleObject.hasOwnProperty(prop)) {
    // Do stuff
  }
}

The Underscore.js library uses the same pattern in its implementation of _.each:

for (var key in obj) {
  if (_.has(obj, key)) {
    if (iterator.call(context, obj[key], key, obj) === breaker) return;
  }
}

Correctly Extending Objects

In ECMAScript 5, properties have an enumerable attribute. If this is set to fault, then the property won’t be included in a loop with for-in. There’s also a method called Object.prototype.propertyIsEnumerable, and methods like Object.keys will respect it.

If I really wanted to add a property to Object, then I could take advantage of this to correctly extend the built-in object:

Object.defineProperty(Object.prototype, 'bad', {
   value: undefined
 , enumerable: false // This property is not enumerable
});

var name, names;

names = {
  bill: { age: 44 }
, bob: { age: 22 }
, john: { age: 29 }
};

for (var name in names) {
  if (names.hasOwnProperty(name)) {
    console.log(name, names[name].age);
  } else {
    console.log('Inherited:', name);
  }
}

Running this example will leave out the bad property.

Notice that this doesn’t just apply to adding properties to native objects – you should also take enumerable into account when adding properties to your own objects.

ECMAScript 5 Array.prototype Methods

ECMAScript 5 introduces some handy methods to Array.prototype:

  • forEach(callback): Run callback over each element
  • filter(callback): Returns a new array for each item where callback returns true
  • map(callback): Run callback over each element, store the result, then return a new array containing the results
  • some(callback): If callback returns true, then stop iterating and return true

There are more methods, including indexOf, lastIndexOf, reduce, and reduceRight. These methods also take a thisArg parameter, which changes the value of this in the callback.

Remember that these are all methods on Array. To use them with an object, one approach is to call Object.keys. The previous example could be rewritten like this:

var name, names;

names = {
  bill: { age: 44 }
, bob: { age: 22 }
, john: { age: 29 }
};

Object.keys(names).forEach(function(name) {
  console.log(name);
});

Scope

In JS101: A Brief Lesson on Scope, I provided an example that showed how creating methods inside loops can cause confusing scope issues. Since forEach uses a callback, we now have a new scope for each iteration:

function example() {
  var o = {};

  [0, 1, 2, 3].forEach(function(i) {
    o[i] = function() { console.log(i); };
  });

  o[0]();
  o[1]();
  o[2]();
}

example();

This will correctly output 0 1 2 rather than the 3 3 3 that was printed with the for example.

Performance

If you’re working with an array of items, a simple for loop will still perform better than forEach:

var length = values.length, i;
for (i = 0; i < length; i++) {
  // Do something
}

There are dozens of benchmarks for this on jsPerf, but in general the good old fashioned for loop will beat forEach.

Relationship to Underscore.js

Underscore.js and jQuery both include methods that work like forEach. This makes it easier to write code that’ll work with older browsers.

Underscore works slightly different to forEach because it doesn’t extend Array.prototype, or any other built-in object. Instead, the array has to be passed as an argument:

_.forEach([0, 1, 2], function(i) {
  console.log(i);
});

The ECMAScript 5 forEach method will be used if it’s available, otherwise a for loop will be used instead.

rAppid:js, Portastic, jstesting

03 Aug 2012 | By Alex Young | Comments | Tags node browser libraries frameworks mvc testing

rAppid:js

rAppid:js (GitHub: it-ony / rAppid.js, License: MIT, npm: rAppid.js) by Tony Findeisen is a new client-side MVC framework that uses XAML for templates. The way the templates work reminds me of AngularJS, but rather than using custom attributes it uses XML documents.

Data can be bound between models and views, and can be set up to work bidirectionally. There’s also a Bindable class that can be used to allow any object to bind to views. Code is kept modular by using AMD and RequireJS. There’s also a Datasource class for calling RESTful data services.

The framework itself is tested with Mocha, but I can’t find anything that specifically addresses testing apps built with the framework. Also, I’m unsure of the relationship between the Node server that can run rAppid.js apps and generating “optimised” builds. The page that’s meant to show help on Node Rendering seems to redirect to the introduction to the project.

Portastic

Portastic (License: MIT, npm: portastic) by Alan Hoffmeister is a port scanner for Node. It’s actually very simple – it uses the async and built-in net modules to check for listening events for each port. It’s not exactly Nmap, so you won’t be using it to perform UDP, NULL, or Xmas scans. However, it is lightweight and includes tests written with Vows, so you could always see what else you can get it to do.

jstesting

jstesting by Bret Little is a project template for writing client-side tests. It includes Jasmine, RequireJS, jsTestDriver, and PhatomJS, and allows test to be run from a static HTML file or as a server.

This also supports headless testing through PhantomJS, so if you’re struggling to write tests for your latest client-side MVC project then this might help.

Backbone.js: Hacker's Guide Part 3

02 Aug 2012 | By Alex Young | Comments | Tags mvc tutorials backbone.js code-review

Last week we continued looking at Backbone.js’s internals, covering collections, and some interesting points about the chainable API design and its relationship to Underscore.js. Before continuing with this week’s post, here are some interesting articles that are in a similar vein to this series:

I’ve already mentioned the first on DailyJS, but I thought I’d mention it again in case you’re still feeling lost about what exactly MVC frameworks can do.

Meanwhile, I’m working on a tutorial series based on Backbone with a focus on combining it with a certain popular user interface component framework, and using a non-REST HTTP API. I’ll release the app itself within the next few weeks.

Router and History

The Backbone.Router constructor function is like the other Backbone classes, based around Underscore’s extend method. Routes are defined with the route method, and it will call _routeToRegExp if the route is a regular expression. This uses isRegExp, which is one of Underscore’s handy type checking methods.

You can use Underscore’s type checking methods in your own projects rather than reinventing them. For example, a lot of projects include a function to check if an argument is an array, but if you’ve already loaded Underscore then _.isArray can be used instead.

Most of what the router does is delegated to Backbone.History. These two classes work together – once a set of routes has been defined and instantiated, then Backbone.history.start must be called. Backbone is probably known for supporting hash URLs at this point, but it also supports the HTML5 History API.

The History class is where Backbone has to do a lot of work to get good cross-browser support, and this is particularly apparent in the start method:

this.options          = _.extend({}, {root: '/'}, this.options, options);
this._wantsHashChange = this.options.hashChange !== false;
this._wantsPushState  = !!this.options.pushState;
this._hasPushState    = !!(this.options.pushState && this.history && this.history.pushState);
var fragment          = this.getFragment();
var docMode           = document.documentMode;
var oldIE             = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));

if (oldIE && this._wantsHashChange) {
  this.iframe = Backbone.$('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
  this.navigate(fragment);
}

To use the History API, pushState: true must be supplied as an option, and the browser must support it. Otherwise, hash URLs will be used, or even iframes. Regardless, the checkUrl method will be called by listening to popstate, onhashchange, or by polling with setInterval.

Like other Backbone methods, start also accepts a silent option. Passing silent: true prevents loadUrl from being called when start is run. This is one of those small touches that shows just how consistent Backbone is.

The Backbone.Router class adds routes by calling Backbone.history.route. The Router class wraps the callback supplied by the user, and the History class adds it to an array of handlers. When the URL changes in the browser, the handlers array is searched, and if a match is found then the callback will run. Due to the way the callback is wrapped by Router, it actually triggers an event.

The beauty of this design is these events can be bound to elsewhere, and the navigate method which changes the current URL is a lot easier to implement:

app.navigate('help/troubleshooting', { trigger: true, replace: true });

This calls the router’s navigate method, which subsequently calls the one in History. URLs can either be replaced or added to the history array.

Views

Backbone.View is a relatively simple class that helps manage view fragments and events. It takes steps to ensure it has a valid DOM element to work with by calling _ensureElement, and includes methods for delegating DOM-related functionality to jQuery-like framework calls in the form of $ and make.

The most interesting part of this class is delegateEvents, which loops over the events property and applies the delegateEventSplitter regular expression to the event property’s keys. This is how Backbone turns things like 'click .button.edit': 'openEditDialog' into the usual jQuery syntax. This method is called by the constructor, and a lot of care is taken to correctly unbind events before adding or altering the view’s main element. You’ll see a few calls to undelegateEvents throughout the class to manage this.

Conclusion

I’ve written Backbone applications without using routers, so it’s interesting to take a deeper look at it and appreciate just how much effort has gone into making it cross-browser.

Although a lot of people see the Backbone.View class as a simple wrapper, it does handle correctly binding and unbinding events, and ensuring there are valid elements to work with.

Node Roundup: BinaryJS, Advice, Buildify, MaDGe

01 Aug 2012 | By Alex Young | Comments | Tags node modules graphics binary websockets
You can send in your Node projects for review through our contact form or @dailyjs.

BinaryJS

BinaryJS (GitHub: binaryjs / binaryjs, License: MIT, npm: binaryjs) by Eric Zhang uses WebSocket to stream binary data. The streams can be bidirectional, and is binary end-to-end. This basically gives browsers something closer to TCP sockets, and allows multimedia data to be streamed.

The client-side portion works with Chrome, Firefox, Internet Explorer 10, and Safari’s nightly builds. The authors are working on supporting older browsers (presumably through Flash).

BinaryJS employs BinaryPack a modified version of the MessagePack protocol. The Node.js server uses a modified version of the ws library enhanced to pass through the status of the socket buffer so adherence to Node.js Stream API is possible.

Advice Functional Mixin

Advice (License: MIT, npm: advice) by Martin Angers is inspired by Angus Croll’s functional mixin example. It can be used to modify an object with after, before, and around methods:

var myObj = { fn: function() {} }
  , withAdvice = require('advice');

withAdvice.call(myObj)
myObj.before(fn, function() {
  // Things that should happen before fn
});

// the 'before' method will now run automatically
myObj.fn();

The author has kindly included thorough Mocha tests as well.

Buildify

Buildify (License: MIT, npm: buildify) by Charles Davison is a build script API:

var buildify = require('buildify');

buildify()
  .load('base.js')
  .concat(['part1.js', 'part2.js'])
  .wrap('../lib/template.js', { version: '1.0' })
  .save('../distribution/output.js')
  .uglify()
  .save('../distribution/output.min.js');

MaDGe

MaDGe (License: MIT, npm: madge) by Patrik Henningsson generates graphs based on CommonJS or AMD dependencies. It can generate various output including text-based lists in the console, and PNGs using Graphviz. There are lots of commnad-line options, which can be loaded from a JSON file.

This example shows the result of using MaDGe with Express:

MaDGe/Express

jQuery Roundup: Flex, jquery-binddata, jquery.notification

31 Jul 2012 | By Alex Young | Comments | Tags jquery plugins ui databinding notifications
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

Flex

jQuery Flex

Flex (GitHub: jasonenglish / jquery-flex, License: MIT) by Jason English displays an animated grid with expanding panels.

The author has based the effect on a design originally implemented with Flash. He was compelled to write the plugin after he found a discussion on Stack Overflow that suggested creating the effect with JavaScript would be impossible.

jquery-binddata

jquery-binddata is a new plugin created by “jdavidw13” that helps bind form fields to simple JavaScript data models. If you’re used to working with MVC frameworks and want to carry across some of the related data binding techniques to a smaller jQuery project, then this plugin might do the trick.

The basic usage is to provide the plugin with an object to bind and a form element:

var data = { field: 'value' };
$('form').binddata(data);

QUnit tests have been included, and the plugin is currently only about 200 lines long.

jquery.notification

jquery.notification (License: MIT) by Mikhail Davydov is a wrapper around the WebKit Notifications API. I’ve seen quite a few of these plugins, but I don’t think I’ve actually seen them used effectively in the wild.

If you’re looking for such a thing, the plugin looks like this:

var notification = $.notification(options, function(isNotificationsAllowed) {
  if (isNotificationsAllowed) {
    notification.show();
  }
});

Rivets.js, BackboneMVC, Unitools, Moog Source

30 Jul 2012 | By Alex Young | Comments | Tags backbone.js mvc unicode library audio

Rivets.js

Rivets.js (GitHub: mikeric / rivets, License: MIT) by Michael Richards is a data binding library that can be used with frameworks like Backbone.js. It has an adapter option that can be set up to support event-based models. Data formatting is supported, so data types like monetary values or dates can be displayed consistently.

Data attributes are used to describe the data bindings in HTML templates. There are built-in bindings like data-html for innerHTML, but tag attributes are also supported, so to write modelInstance.url to an image’s src attribute data-src could be used.

Rivets.js embraces a pipe-inspired syntax for certain operations. For example, rendering a collection could automatically sort the results like this:

<ul data-html="model.tags | sort | tagList"></ul>

BackboneMVC

BackboneMVC (GitHub: chance-an / backbone-mvc, License: LGPL) by Changsi An adds a controller to Backbone. Controllers are singletons, which means the same instance will always be returned. I suspect this might fit well if you’re wrapping classes with AMD and loading them with RequireJS.

Methods on controllers can be executed by Backbone.Router, so it works very much like Rails (the author states the project is inspired by CakePHP). The beforeFilter and afterRender hooks are executed based on a jQuery Deferred object, but false can be returned as well.

Unitools

Unitools is a collection of Unicode web-related tools for creating all manner of useful and ludicrous text effects.

It includes the Twitter favourite upside down mapping (”sɾʎlıɐp ɯoɹɟ ollǝɥ”), Zalgo, and tools for escaping and working with raw Unicode values.

Moog Google Doodle Open Sourced

I noticed the Moog Google Doodle has been released under the Apache 2.0 license here: bob-moog-google-doodle (HN discussion). I was coincidentally recently working on some web audio stuff, so it’s useful to have something like this to refer to.

TiddlyWiki5, Backbone.Notifier, MVC Jungle

27 Jul 2012 | By Alex Young | Comments | Tags apps mvc backbone.js libraries

TiddlyWiki5

TiddlyWiki5 (GitHub: Jermolene / TiddlyWiki5, npm: tiddlywikigithub) by Jeremy Ruston is a reboot of the venerable TiddlyWiki. As reboots go I’d say this was more along the lines of Batman Begins than The Amazing Spider-Man. It can run entirely in a browser, or as a Node application.

TiddlyWiki is designed to fit around your brain, giving you a better way of managing data compared to traditional documents and emails. The fundamental idea is that information is more useful and reusable if we cut it up into the smallest semantically meaningful chunks.

The project is currently tentatively released as an alpha, and you can keep track of future updates on the TiddlyWikiDev Google Group and @TiddlyWiki on Twitter.

Backbone.Notifier

Backbone.Notifier (License: MIT, GitHub: ewebdev / backbone.notifier) by Eyal Weiss is a notification library designed to work with Backbone.js. Features include dialogs, modal display, and CSS3-based effects.

The Backbone.Notifier class works like the other Backbone classes:

var notifier = new Backbone.Notifier(options);
nofitier.notity('What did one snowman say to the other snowman?');

Instances of Backbone.Notifier can be chained, and the class is event-based, which means it’s quite easy to create wizards by chaining together multiple dialogs.

Journey Through The JavaScript MVC Jungle

Journey Through The JavaScript MVC Jungle is a detailed article by Addy Osmani that introduces MVC frameworks and his TodoMVC collection of demos. There are also some handy descriptions of when to use each framework:

Use KnockoutJS: I want something that will help me build simple Web applications and websites. I don’t expect there to be a great deal of code involved and so code organisation won’t be much of a concern.

Backbone.js: Hacker's Guide Part 2

26 Jul 2012 | By Alex Young | Comments | Tags mvc tutorials backbone.js code-review

Last week we looked at Backbone.js’s internals, covering configuration, server support, events, and models. I actually really enjoy looking at projects this way, it’s one of the best ways to learn new programming techniques. So let’s continue dissecting Backbone by taking a look at Backbone.Collection.

Constructor

Backbone.Collection is a constructor function that accepts an array of models and an options object.

As an aside, notice that void 0 is used in this code. To understand why, recall that the void operator returns undefined. Since ECMAScript 5, the undefined property isn’t writable, so it’s safe to use it. However, in earlier versions it was writable, which meant malicious code could technically take advantage of this fact by assigning a value to the undefined property of the global object. The void operator expects an expression, so void 0 is considered the idiomatic way of safely obtaining undefined.

The constructor calls the reset method, which removes existing models and adds new ones. This is similar to instantiating a collection with no models, and then manually calling add on each one.

Inheritance and Mixins

The Collection class inherits from Backbone.Events. Events are used both publicly and internally. There’s a toJSON method that iterates over each model and calls the model’s toJSON method. This brings up an interesting point: collections use methods from Underscore.js, but Collection doesn’t inherit from Underscore. Why not? Well, certain methods are manually assigned to Collection.prototype, while others are rewritten in ways that make sense in Backbone. For example, the pluck method works on model attributes, and sort uses the boundComparator which has a slightly different API to Array.prototype.sort.

Adding and Removing Items

Collections are basically an array of models with events, wrapped with convenient Underscore-like iterator methods. The add method is always called however models are added, which means it’s a good place to do housekeeping like preventing invalid models and duplicates from being inserted into the collection. Models are also indexed by id, and all model events are bound to _onModelEvent. This method dynamically adds new models, removes deleted ones, and updates models with changes.

If the collection requires sorting, the add method will call sort once all models have been processed. And, if the silent option isn’t set, an add event will be triggered for each model that was successfully added.

It naturally follows that the remove method has a fair amount of work to do, given the complexity of add. The indexed ids must be deleted, and _removeReference is called to remove the model’s reference back to the collection.

Deleting items in JavaScript is interesting, because we actually have the delete keyword to do this for us. However, delete is only used for properties, so the authors have used the Array.prototype.splice technique to delete models from the array. The add and remove methods also update the length property, which allows the collection to behave in an Array-like manner, and helps support the mixed-in Underscore methods.

Now take a look at the simplicity of the where method. It basically loops over each model, comparing an attributes object. This is simple because the filter method is taken directly from Underscore.

Chainable API

Another bit of sugar is the support for Underscore’s chain method. This works by calling the original method with the current array of models and returning the result. In case you haven’t seen it before, the chainable API looks like this:

var collection = new Backbone.Collection([
  { name: 'Tim', age: 5 },
  { name: 'Ida', age: 26 },
  { name: 'Rob', age: 55 }
]);

collection.chain()
  .filter(function(item) { return item.get('age') > 10; })
  .map(function(item) { return item.get('name'); })
  .value();

// Will return ['Ida', 'Rob']

Some of the Backbone-specific method will return this, which means they can be chained as well:

var collection = new Backbone.Collection();

collection
    .add({ name: 'John', age: 23 })
    .add({ name: 'Harry', age: 33 })
    .add({ name: 'Steve', age: 41 });

collection.pluck('name');
// ['John', 'Harry', 'Steve']

Conclusion

I’ve been using Backbone for a while, and I’ve never really thought about how the Backbone.Collection methods can be chained. Sometimes it’s difficult to tell what’s possible though – once you’re in an Underscore chain you can’t use methods like pluck because Backbone’s models use the get method to access attributes, so you’ll end up with an array of undefined values.

Next week I’ll continue looking at Backbone by investing the formidable routing and history APIs.

Node Roundup: Node 0.9.0, ttycast, Simba

25 Jul 2012 | By Alex Young | Comments | Tags node modules terminal config
You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.9.0

Node 0.9.0 is out, and this marks the first in the 0.9 series of unstable releases. In the recent commits there’s been an interesting discussion about uncaughtException:

So if uncaughtException may be removed in the future, does that mean every node.js process will have at least 1 domain?

Node 0.8.3 is the latest addition to the stable line, and this release adds a lot of fixes:

  • net: fix net.Server.listen({fd:x}) error reporting (Ben Noordhuis)
  • net: fix bogus errno reporting (Ben Noordhuis)
  • build: fix add-on loading on freebsd (Ben Noordhuis)
  • build: fix spurious mksnapshot crashes for good (Ben Noordhuis)
  • domain: Fix memory leak on error (isaacs)
  • events: Fix memory leak from removeAllListeners (Nathan Rajlich)
  • zlib: Fix memory leak in Unzip class. (isaacs)
  • crypto: Fix memory leak in DecipherUpdate() (Ben Noordhuis)

ttycast

ttycast

ttycast (GitHub: dtinth / ttycast, License: MIT, npm: ttycast) by Thai Pangsakulyanont is a terminal-to-web broadcasting utility built with ttyrec, Connect, and Socket.IO.

Once ttyrec and ttycast are installed, activity within a terminal can be viewed through a browser. The author has written up some tips on how to do this over SSH.

I tried it out, but I had a problem running it due to a Connect version issue. I’ve let the author know through GitHub so hopefully it’ll work out of the box fairly soon.

Simba

Simba (License: MIT, npm: simba) by Charles Bourasseau is a configuration management module:

simba
  .add('db')
  .children() 
    .add('host', 'localhost')
    .add('port')
    .add('username')
    .add('password')
  .end();

console.log(simba.get('db').get('host')); // 'localhost'

There is full documentation available for Simba that explains some of the underlying concepts and each API method.

jQuery Roundup: noty, jquery.tocify.js, Routie

24 Jul 2012 | By Alex Young | Comments | Tags jquery plugins routing ui navigation
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

noty

noty

noty (GitHub: needim / noty, License: MIT) by Nedim Arabacı is a plugin for displaying notifications. It supports alerts with the usual set of levels, and also allows a prompt to be displayed. A “noty” can be created using a function that accepts an options object:

var noty = noty({ text: 'noty - a jquery notification library!' });

An existing container can be populated using the familiar jQuery syntax:

$('.custom_container').noty({ text: 'noty - a jquery notification library!' });

Notifications can be queued, allowing a large amount of alerts to be handled sensibly. This plugin also supports themes through CSS or a JavaScript file.

jquery.tocify.js

jquery.tocify.js (GitHub: gfranko / jquery.tocify.js, License: MIT) by Greg Franko is a table of contents plugin that works with jQuery UI’s ThemeRoller and jQuery’s animation effects. It also supports history.pushState, so pressing the back button will work as expected.

Given a suitable container div, running $('#toc').tocify(); will generate a table of contents based on the headers on the page.

Routie

Routie (GitHub: jgallen23 / routie, License: MIT) by Greg Allen is a lightweight hash routing library. It’s not specifically dependent on jQuery, and the author has packaged it nicely with a makefile and build instructions. Routes can be defined as follows:

routie('users', function() {
  //this gets called when hash == #users
});

routie({
  'users': function() {
  },
  'about': function() {
  }
});

It also works with regular expressions to allow parameters to be accessed. Notice how parameters are mapped to the function arguments:

routie('users/:name', function(name) {
});

The project includes tests written with Mocha and Chai.

JS101: A Brief Lesson on Scope

23 Jul 2012 | By Alex Young | Comments | Tags js101 tutorials language beginner

JavaScript is a language that demands a rigorous understanding of its scoping rules. One reason for this is JavaScript looks deceptively like other languages, but subtle differences in the rules that govern identifier visibility make it unexpectedly difficult to master.

Here’s some computer science so you can impress your friends and wayward wizards: scope refers to the visibility of a given identifier within a program. Sometimes we talk about function scope and block scope. I’ve already covered function scope in this series, but JavaScript doesn’t have block scope. In this context, block refers to control structures like if statements and for loops – a block of statements grouped by curly braces.

Declaring Variables and Functions

Variables can be defined with the var statement, and are initialised to undefined:

var a; // undefined
var b = 'hello';

The scoping of these statements is dependent on where they’re declared. If var statements don’t appear inside a function, they’re globally accessible:

var a = 1;

function sum(b) {
  return a + b;
}

Missing var

Problems start to occur when a var statement is forgotten:

function example() {
  a = 1;
  b = 1;
  return a + b;
}

These variables are not local to the example function, they’re actually global. If a or b already existed, then their values will be overwritten.

Accidentally leaving out a var statement is surprisingly easy, and could potentially cause irritating bugs. Tools like JSLint will attempt to use static analysis to find such errors.

Variable Declaration Styles

Some people like to group var statements together:

var a = 1,
    b = 2,
    c = 3;

What would happen if a comma was missed by mistake?

function example() {
  var a = 1
      b = 2,
      c = 3;
}

example();

console.log(typeof a);
console.log(typeof b);
console.log(typeof c);

Running this will show that while a is undefined and therefore local to example, the other variables are global.

The reason some developers place commas before lists of variable declarations is to make it easier to see if a comma has been forgotten:

var a = 1
  , b = 2
  , c = 3
  ;

However, the Google JavaScript Style Guide recommends using a var statement on every line, to avoid the problem altogether.

No Block Scope

Variables and functions are visible within the current function, regardless of blocks. This is amazingly confusing because it prevents us from using control structures to declare functions and variables in a dynamic way.

Defining variables in blocks may confuse programmers who work with other languages:

function example() {
  // Do not do this
  for (var i = 0; i < 3; i++) {
    var a = 1;
    // Do stuff with `a`
  }
}

Since there is no block scope, the previous example should be written like this:

function example() {
  var i, a;
  for (i = 0; i < 3; i++) {
    a = 1;
    // Do stuff with `a`
  }
}

Hoisting

Have you ever noticed how some things appear to be in scope even though their definition appears later in the file? The colloquial term for this is hoisting:

function example() {
  console.log(a);
  var a = 1;
}

example();

Running this will log undefined rather than 1. The term hoisting isn’t in the ECMAScript 3 or 5 standards, but the behaviour is documented in 10.5 Declaration Binding Instantiation, in the line that starts For eachVariableDeclaration_ and VariableDeclarationNoIn d in code, in source text order do_. The reason the value is undefined rather than 1 is also explained by the specification:

A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

Confusing Closures

The following example defines three functions and assigns them as methods to an object. Each method is then called after the loop has finished.

function example() {
  var o = {}, i = 0;
  for (i = 0; i < 3; i++) {
    o[i] = function() { console.log(i); };
  }
  o[0]();
  o[1]();
  o[2]();
}

example();

The output will be 3 each time, because the closure is bound to the function scope, not the (non-existent) block scope. Intermediate JavaScript programmers often make this mistake.

Conclusion

If understanding JavaScript’s scoping rules was a video game, then the levels would be as follows:

  • Title Screen: Press [Start] to begin (I always press the other buttons to see if it starts anyway)
  • Level 1: Function scope
  • Level 2: No block scope
  • Level 3: Missing var
  • Level 4: Hoisting
  • Level 5: Closures

The scoping story isn’t complete, and worth noting is the fact that ECMAScript extensions can subtly alter scoping behaviour for certain things. If you’re interested in going beyond this 101 introduction level article, then try reading Angus Croll’s Function Declarations vs. Function Expressions to see how some of these scoping rules play out, and Dmitry Soshnikov’s post on ECMAScript’s lexical environments.

References

Enyo, Miso, Dancer.js

20 Jul 2012 | By Alex Young | Comments | Tags libraries frameworks webgl audio

Enyo 2.0

Enyo 2 (GitHub: enyojs, License: Apache 2.0) from HP was released this week. There’s a post all about the release on the Enyo blog:

Enyo 2 boasts an amazing community of developers, a broad set of cross-platform UI widgets, and a powerful layout library for building apps that work across all form factors from phones to desktops.

They’re also promising more updates, with some great hyperbole about the future of web apps:

We see a web-centric future in which there aren’t iOS apps, Android apps, Mac apps and Windows apps - there are just apps: apps that let you access your content and get stuff done, wherever you happen to be, on whatever device is handy.

Miso Dataset

Miso Dataset Example

Miso Dataset (GitHub: misoproject / dataset, License: MIT/GPL, npm: miso.dataset) is a client-side library for managing data. There’s an example called Exploring government spending that uses D3 and Backbone.js alongside Miso Dataset to generate an interactive chart that reminds me of the images David McCandless creates.

Dataset provides tools for working with data in a web-friendly, event-based manner. Data can be loaded using techniques like jsonp, and then manipulated in a similar way to Backbone’s models (Dataset also has an event-based syncing system). It comes with extensible classes for parsing and displaying data, and it also plays well with Node.

There are Dataset tutorials, and a Quick Start guide for the perplexed.

Dataset is actually part of a wider project called Miso by the Guardian Interactive team and Bocoup.

Thanks to @flywheeltech for sending this in.

dancer.js

dancer.js (GitHub: jsantell / dancer.js, License: MIT) by Jordan Santell is an audio API designed for making visualisations. It can work with the HTML5 Audio or use a Flash fallback. It actually includes stuff like an FFT class from DSP.js, so it allows interesting effects to be created while working at a relatively high level.

Backbone.js: Hacker's Guide

19 Jul 2012 | By Alex Young | Comments | Tags mvc tutorials backbone.js code-review

There’s no denying the popularity and impact that Backbone.js (License: MIT, GitHub: documentcloud / backbone) by Jeremy Ashkenas and DocumentCloud has made. Although the documentation and examples are excellent, I thought it would be interesting to review the code on a more technical level. Hopefully this will give readers a deeper understanding of Backbone, and as the MVC series progresses these code reviews should prove useful in accurately comparing the many competing frameworks.

Follow me on a guided tour through Backbone’s source to really learn how it works and what it provides.

Namespace and Conflict Management

Like most client-side projects, Backbone.js wraps everything in an immediately-invoked function expression:

(function(){
  // Backbone.js
}).call(this);

Several things happen during this configuration stage. A Backbone “namespace” is created, and multiple versions of Backbone on the same page are supported through the noConflict mode:

var root = this;
var previousBackbone = root.Backbone;

Backbone.noConflict = function() {
  root.Backbone = previousBackbone;
  return this;
};

Multiple versions of Backbone can be used on the same page by calling noConflict like this:

var Backbone19 = Backbone.noConflict();
// Backbone19 refers to the most recently loaded version,
// and `window.Backbone` will be restored to the previously
// loaded version

This initial configuration code also supports CommonJS modules so Backbone can be used in Node projects:

var Backbone;
if (typeof exports !== 'undefined') {
  Backbone = exports;
} else {
  Backbone = root.Backbone = {};
}

The existence of Underscore.js (also by DocumentCloud) and a jQuery-like library is checked as well.

Server Support

During configuration, Backbone sets a variable to denote if extended HTTP methods are supported by the server. Another setting controls if the server understands the correct MIME type for JSON:

Backbone.emulateHTTP = false;
Backbone.emulateJSON = false;

The Backbone.sync method that uses these values is actually an integral part of Backbone.js. A jQuery-like ajax method is assumed, so HTTP parameters are organised based on jQuery’s API. Searching through the code for calls to the sync method show it’s used whenever a model is saved, fetched, or deleted (destroyed).

What if jQuery’s ajax API isn’t appropriate for your project? Well, it seems like the sync method is the right place to override for changing how models are persisted, and this is confirmed by Backbone’s documentation:

The sync function may be overriden globally as Backbone.sync, or at a finer-grained level, by adding a sync function to a Backbone collection or to an individual model.

There’s no fancy plugin API for adding a persistence layer – simply override Backbone.sync with the same function signature:

Backbone.sync = function(method, model, options) {
};

The default methodMap is useful for working out what the method argument does:

var methodMap = {
  'create': 'POST',
  'update': 'PUT',
  'delete': 'DELETE',
  'read':   'GET'
};

Events

Backbone has a built-in module for handling events. It’s a simple object with the following methods:

  • on: function(events, callback, context) , aliased to bind
  • off: function(events, callback, context) {, aliased to unbind
  • trigger: function(events) {

Each of these methods returns this, so it’s a chainable object. The comments recommend using Underscore.js to add Backbone.Events to any object:

//     var object = {};
//     _.extend(object, Backbone.Events);
//     object.on('expand', function(){ alert('expanded'); });
//     object.trigger('expand');

This won’t overwrite the existing object, it appends the methods instead. That means it’s easy to add event support to other objects in your project.

Model

Backbone.Model is where things start to get serious. Models use a constructor function that sets up various internal properties for managing things like attributes and whether or not the model has been saved yet. Underscore.js is used to add the methods from Backbone.Events, and then the public model API is defined. This contains most of the frequently used Backbone methods.

Notice that Backbone.Model is actually quite transparent: there aren’t any private methods defined inside the constructor.

The set method supports two different signatures, making it easy to support a single attribute or multiple attributes:

// Handle both `"key", value` and `{key: value}` -style arguments.
if (_.isObject(key) || key == null) {
  attrs = key;
  options = value;
} else {
  attrs = {};
  attrs[key] = value;
}

The save method does something similar. Notice how the authors ensure an object is always set for options:

options || (options = {});

In terms of expressing the programmer’s intent, this seems better than options = options || {}.

The set method triggers validations and prevents the method from progressing if a validation fails:

if (!this._validate(attrs, options)) return false;

Next each attribute is iterated over. If the attribute has changed, according to Underscore’s isEqual method, then the change is recorded. Once the list of changes have been built, the change method is called.

The change method calls trigger for each change. This allows for changes to any attribute to be listened on specifically, allowing the UI to be updated appropriately. For example, let’s say I had a blogPost model instance:

blogPost.on('change:title', function() {
  // Update the HTML for the page title
});

blogPost.set('title', 'All Work and No Play Makes Blank a Blank Blank');

Other methods also trigger change events: unset, clear, and fetch. Since we don’t always care if these cause a change event, a silent option is supported that will be passed from these methods to set. It’s actually quite interesting how each of these methods is implemented by reusing set:

// Clear all attributes on the model, firing `"change"` unless you choose
// to silence it.
clear: function(options) {
  options = _.extend({}, options, {unset: true});
  return this.set(_.clone(this.attributes), options);
},

The fetch method will trigger a sync operation that will retrieve the latest values from the server (or suitable persistence layer if it’s been overridden).

The save method ensures only valid attributes and models are persisted, and calls set if required:

if (options.wait) {
  if (!this._validate(attrs, options)) return false;
  current = _.clone(this.attributes);
}

// Regular saves `set` attributes before persisting to the server.
var silentOptions = _.extend({}, options, {silent: true});
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
  return false;
}

// Do not persist invalid models.
if (!attrs && !this.isValid()) return false;

The sync method is called to persist the changes to the server. isNew is used to determine if the model should be created or updated. The isNew state is determined by whether an id attribute exists or not. This could be easily overridden if a given persistence layer works a different way. Notice that Backbone internally references this attribute as this.id and doesn’t map it to the value set with idAttribute in isNew.

A parse placeholder method is called whenever models are fetched, or saved. There are examples of people using this to parse other data formats like XML.

Conclusion

After looking at the Backbone.js setup and model code, we’ve already learned quite a lot:

  • Any persistence scheme can be supported by overriding the sync method
  • Models are event-based
  • change events can drive the UI whenever models change
  • Models know when to create or update objects
  • Reusing Backbone’s models, events, and Underscore methods is useful for organising project architecture

Although the Backbone models don’t have a plugin layer, the authors have kept the design open and allowed for just the right hooks to support lots of HTTP services and data types outside the built-in RESTful JSON oriented design.

Backbone relies heavily on Underscore.js, which means applications built with it can build on both of these libraries to create (potentially) well-designed and reusable code.