Posted by Derek on Friday, March 18, 2011.

Carbon.js

Lately, I’ve been working on a Google Maps mashup that calculates CO2 emissions for automobile, bus, and rail trips. Since Google Maps has such a great JavaScript API, I decided to write the application almost entirely in JavaScript. Thus, Carbon.js was born!

Carbon.js makes it simple to calculate emissions. Its design is similar to the Ruby carbon gem. For any class that represents an “emission activity”, e.g. an automobile trip, you can extend it with Carbon.js to give it the ability to perform calculations.

Let’s look at an example.

RentalCar = function() {};
Carbon.emitter(RentalCar, function(emitter) {
  emitter.emitAs('automobile');
  emitter.provide('make_model');
  emitter.provide('weekly_distance');
  emitter.provide('timeframe');
});

var focus = new RentalCar;
focus.make_model = 'Ford Focus';
focus.weekly_distance = 436;
focus.timeframe = '2010-03-08/2010-03-14';

focus.getEmissionEstimate(function(estimate) {
  alert("My Focus' emissions for the rental period of March 8th-14th are: " + estimate.value());
});

As you can see, the configuration defined in the Carbon.emitter call maps properties on an instance of RentalCar to characteristics defined in automobile’s characteristics API. In order to calculate emissions, a call to getEmissionEstimate with a callback function parameter is required. Within that callback, you can update HTML on a page with the result.

Future Improvements

  • Carbon only maps properties of an object to characteristics. It will be simple to allow an instance function to be mapped to a characteristic.
  • Carbon.js requires that you use jQuery. One improvement would be to use platform-agnostic XHR and proxy functions, or intelligently detect available libraries, like Prototype, MooTools, etc.
  • For DRYness sake, Carbon.js operates on classes. It may be helpful to be able to decorate any JavaScript object on the fly.

What blog is this?

Safety in Numbers is Brighter Planet's blog about climate science, Ruby, Rails, data, transparency, and, well, us.

Who's behind this?

We're Brighter Planet, the world's leading computational sustainability platform.

Who's blogging here?

  1. Patti Prairie CEO