Prototypes

FIXME

IGNORE THE FOLLOWING NOTES FOR NOW, AND JUST TAKE A LOOK AT THE SAMPLE CODE !!

Prototypes 
    persisting; fetching; removing 
    folders 
    fetching objects via ID 
    Actions: changes are just effective if no errors occur (res.abort, res.commit) 
    constructor() 
    inheritance - HopObject 
    HopObject 
        .add 
        .remove 
        .get 
        HopObject.getById()

Explain everything before we actually start showing some sample code; we use internal xml-based db for now;

Explain what we want to do in the sample app: Create a single prototype, create/persist, fetch, modify, and remove that object; List all existing persons with link to modification page, resp to delete-action; 

Create folder named 'Person';

Create minimalistic rendering framework, i.e. a simple global main-skin with res.data.body;

Root/main_action: loops through all existing persons, and builds string for res.data.body with id, name, Height and DateOfBirth

Root/createPerson_action: builds string with simple form to insert name, dateOfBirth and size with target=createPerson; if no name, then display message; otherwise create and persist new Person and redirect to main_action;

Insert link to deletePerson-action with personId as url-parameter in the Root/main_action-list

Root/deletePerson_action expects personId to be passed as url-string; writes out error, if no person is found; otherwise deletes that Person and redirects to root/main_action

Explain: how to fetch a persistent object, and how to delete it;

Insert link to Person/edit-action; contains a small form to change the name of that person; redirects to Root/main_action after successful update;

Explain: modification of existing objects;

Explain constructor(), HopObject-inheritance

calls list.skin;lists all existing person

Create root-action with small form to create new Person with certain 'name', and make that persistent; Root/createPerson_action();

create prototype 'person'

hopobject-functions; HopObject-inheritance

properties id, name, dateofbirth, height

create Root/main_action that creates and lists all Persons

person-skin, person-macro, person-function

person-action; _parent ?? person-url?

href_macro ??

demoPrototypes.zip

### HopObject/functions.js ### 
 
function info_action() { 
   res.write("HELLO " + this._prototype.toUpperCase() + " " + this._id); 
} 
 
 
### Root/functions.js ### 
 
function main_action() { 
   // list all Persons 
   for (var i=0; i<root.count(); i++) { 
      var p = root.get(i); 
      res.write("<li>"); 
      res.write(p.name + " "); 
      if (p.dateofBirth) res.write(p.dateOfBirth.format("dd.MM.yyyy") + " "); 
      res.write("<a href=" + this.href("deletePerson") + 
         "?personId=" + p._id + ">delete this Person</a> ");
      res.write("<a href=" + p.href("edit") + ">edit this Person</a>"); 
   } 
   res.write("<br />");
   res.write("<a href=" + this.href("createPerson") + ">create new Person</a>");
} 
 

function createPerson_action() { 
   if (req.data.send) { 
      // instantiate a new object of type 'Person' 
      var p = new Person(); 
      // set the property 'name' of the new Person 
      p.name = req.data.name; 
      // set a integer value 
      p.bodySize = 180; 
      // set the date of birth 
      p.dateOfBirth = new Date(1978, 4, 24); 
      // store the new object persistently 
      root.add(p); 
      // now redirect the client to the list of all Persons 
      res.redirect(this.href("main")); 
   } 
   this.renderSkin("createPerson"); 
} 
 
 
function deletePerson_action() { 
   // if personId is passed in URL, and such a Person exists... 
   if (req.data.personId && root.get(req.data.personId)) { 
      // ... we delete that Person 
      var p = root.get(req.data.personId); 
      p.remove(); 
   } 
   // now redirect the client to the list of all Persons 
   res.redirect(this.href("main")); 
} 
 
 
### Root/createPerson.skin ### 
 
<form action=""> 
  <input type="text" name="name"> 
  <input type="submit" name="send" value="create new Person"> 
</form> 
 
 
### Person/functions.js ### 
 
function edit_action() { 
   // if the form has been submitted... 
   if (req.data.send) { 
      // ... change the name of that person 
      this.name = req.data.name; 
      // ... and redirect to the root main action 
      res.redirect(root.href("main")); 
   } 
   this.renderSkin("edit"); 
} 
 
 
function constructor() {
   // a constructor can be optionally defined for each
   // prototype in order to have programm code being
   // executed whenever a new Object is instantiated
   ;
} 
 
 
### Person/edit.skin ### 
 
<form action=""> 
  <input type="text" name="name"> 
  <input type="submit" name="send" value="change name"> 
</form>