Index ¦ Archives  ¦ Atom  ¦ RSS

Navigating WebOS Node.JS services Part 1

This is actually a continuing from an earlier post regarding Enyo and services, but since Enyo is in NDA, I'm going to assume you know how to get the hello world service working and can create your own service from scratch.

I’ve written two small helpers to distinguish service and command assistants, as well as setup the necessary bits I need for them:

var createService = function(name, p) {
    this[name] = function(){};
    this[name].prototype = p;
    this[name].prototype.setup = this[name].prototype.setup || function(){};
};
var createFunction = function(name, p) {
    this[name] = function() {};
    this[name].prototype= p;
};

These are simply called with the name of the assistant to create, and it’s prototype:

createService("servicename",{
  setup: function(){
    console.log("servicename has been setup");
  }
});

createFunction("commandname",{
  run: function(future){
    console.log("servicename's commandname has been called with these arguments:");
    for(var arg in this.controller.args){
      console.log(arg + ": " + this.controller.args[arg]);
    }
    future.result = {reply:"commandname returning"};
  };
});

Now, I'd like to add an AjaxCall. I had to look through MojoSyncFramework/plaxo.srv and I've found the bare minimum to add an AjaxCall.

createFunction("commandname",{
  run:function(future){
    var ajaxcall = AjaxCall.get("http://fahhem.com/");
    future.nest(ajaxcall).then(
      function() { // success function
        future.result = future.result;
      },
      function() { // error function
        future.result = {;
      }
  }
});

That seems straightforward, but it took a lot of trial and error to get it down from plaxo.srv to that. For one, plaxo.srv had an extra intermediary Future that was totally unnecessary, but did teach a little about futures that I can use later. Good to know embedded programming is as high-quality as always.

Just having that command isn't sufficient, if you look closely I used "AjaxCall", which isn't in any context available normally. To make it available, the sources.json must include the following line before the line including the above createFunction() call:

{"library":{"name":"foundations","version":"1.0"}},

That line provides IMPORTS.foundations, so to get AjaxCall, include these lines somewhere before you need it, preferable in a prologue.js file as I've seen done to keep the clutter out when building larger services.

var Foundations = IMPORTS.foundations;
var AjaxCall = Foundations.Comms.AjaxCall;

More documentation on AjaxCall, such as options that can be provided to AjaxCall.get(): https://developer.palm.com/content/api/reference/javascript-libraries/foundations/foundations-comms-ajax-call.html

Good luck with your Node.JS woes!

© Fahrzin Hemmati. Built using Pelican. Theme by Giulio Fidente on github.