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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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.