zimki | documentation | search
 
Zimki Documentation - CookBook.ListingInstances

Start with the CookBook.CreatingClasses example. Then amend the Main javascript instance thus:

zimki.library.require('trimpath.js');

// create a class called Visitor if it doesn't exist yet
try {
    zimki.persist.create('Visitor');
}
// if the class already exists, we'll get a zimki.error.class exception, which
// we can safely ignore.
catch(e) {} 

function hello(args) {
    var data = {};
    data.who = !args.who ? 'Superman' : args.who;

    // create a new instance for the current visitor
    var person = Visitor.create({ where:'uk', name: data.who });

    // Put a list of all our visitors into the data dictionary
    data.visitors = Visitor.search({});

    var pagecount = Counter.search({ name: 'page' })[0];
    pagecount.count++;
    pagecount.save();

    data.pagecount = pagecount.count;
    return zimki.render.trimpath('hello', data );
}
zimki.publishPath("/", hello);

Now, amend the hello template to read the following:

<html>
<head>
  <title>my greeting</title>
</head>
<body>
  <h1>Hello ${who}</h1>
  <p>Pagecount: ${pagecount}</p>
  
  <p>Visitors: </p>
  <ol>
  {for visitor in visitors}
     <li>${visitor.name}</li>
  {/for}
  </ol>
</body>
</html>

Next step

Notice, if you try this multiple times with different names, that you may not get the entries back in the order you expect. To get the visitors back in a consistent order, you can replace the line

data.visitors = Visitor.search({}); 

with

data.visitors = Visitor.search({ order_by: '-creation_date' }); 

to get the visitors back ordered by time, last entry first.

See also

Parent

CookBook