Recent updates to the Kynetx platform related to Hash expressions has given me the opportunity to revisit my previous post. If your WebApp has modest requirements for storage of data it is worthwhile considering the use of Persistent Variables. To demonstrate the salient principles of using persistent variables as a datastore, a simple Kynetx WebApp addressbook is provided.



The addressbook application stores the name, email and phone number of each person. The addressbook data is stored in a single persistant variable as key/value pairs. With the persons name used as the key:

"SomeName" : {
    "Name"  : "SomeName",
    "Email" : "SomeEmail",
    "Phone" : "SomePhone"
}

Since there is a lot of to wade through, so let me point out that parts specifically related to reading, writing and remove entries in the addressbook. In order to read all of the entries in the addressbook the following ruleset is used:

foreach ent:KRUDaddresss.keys() setting (addressKey)
pre {
  email = ent:KRUDaddresss{[addressKey, "Email"]};
  phone = ent:KRUDaddresss{[addressKey, "Phone"]};
  addressRow = <<
      
          #{addressKey}
          #{email}
          #{phone}
          Remove
      
  >>;
}
{
  append('#panelAddressList .DataTable tbody', addressRow);
}

New entries for the addressbook are entered via a standard HTML form. With a watch() placed on the submit the following rule captures the entry from the form, builds a new entry and adds it to the persistent variable:

select when submit "#formAddressAdd"
pre {
  formName  = event:param("formName");
  formEmail = event:param("formEmail");
  formPhone = event:param("formPhone");
  formHash = {
      "Name"  : formName,
      "Email" : formEmail,
      "Phone" : formPhone
  };

  KRUDaddresss = ent:KRUDaddresss || {};
}
{ noop(); }
fired {
  set ent:KRUDaddresss{[formName]} formHash;
}

Entries are removed from the addressbook by calling the following rule with an entries name as the event parameter:

pre {
  keyName = event:param("keyName");
}
{ noop(); }
fired {
  clear ent:KRUDaddresss{keyName};
}

You can play with the online demo to see the end results. And you can get the source code for the whole WebApp on GitHub. Heads up, there's a lot of code in the WebApp related to the GUI, that's why I wanted to call out the salient CRUD elements above. Enjoy!

11 Oct 2011: Revised code for adding new addressbook entries to use the new expression syntax for Persistent Hashes. When these new expressions are used you avoid loading, binding and transferring the entire hash.