node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป


Apache Cordova Meaning Of Life Plugin

This project contains a simple JavaScript-only plugin for Apache Cordova. I created this plugin to use as one of the examples in chapter 16 of my Apache Cordova 4 Programming book. The plugin calculates the "Answer to The Ultimate Question of Life, the Universe, and Everything" (From Douglas Adams' The Hitchhiker's Guide to the Galaxy). Basically, when a Cordova app invokes one of the methods, the plugin returns the number 42. You'll have to be a fan of the book to understand why. :-)


To add the plugin to one of your Cordova projects, open a terminal window, navigate to the Cordova project folder, and execute the following command:

cordova plugin add johnwargo-cordova-plugin-mol


The plugin exposes two methods:

  • calculateMOL - Asynchronous, returns its result through execution of a callback function
  • calculateMOLSync - Synchronous, returns its result immediately

To use the Async version, execute the calculateMOL method, and, in the call's parameters, pass in a function you want executed with the result:

function doMOLAsync() {  
  var res = mol.calculateMOL(molCallback);

function molCallback(res) {  
  var msg = "Result: " + res;
  navigator.notification.alert(msg, null, "MOL", "Continue");
  console.log("Leaving molCallback");

To use the synchronous version, simply call the calculateMOLSync method and process the result returned by the method:

function doMOLSync() {
  var res = mol.calculateMOLSync();
  var msg = "Result: " + res;
  navigator.notification.alert(msg, null, "MOL", "Continue");

Is this a useful plugin? No. I simply wrote it to show that it's possible to create a Cordova plugin that doesn't have any native platform code in it.

If you find this code useful, and feel like thanking me for providing it, please consider making a purchase from my Amazon Wish List. You can find information on many different topics on my personal blog. Learn about all of my publications at John Wargo Books.