root/sandbox/minibase/trunk/demo/model.js
| Revision 8915, 1.4 kB (checked in by hannes, 2 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | // simple demo model for helma minibase |
| 2 | importModule('helma.minibase', 'db'); |
| 3 | |
| 4 | /** |
| 5 | * Our model class. The only thing to observe is to return a Storable |
| 6 | * instance, passing this object and an object containing the persistent |
| 7 | * properties to the Storable constructor. |
| 8 | * |
| 9 | * @param properties persistent properties container |
| 10 | */ |
| 11 | function Book(properties) { |
| 12 | |
| 13 | // Define any instance methods you like, accessing persistent |
| 14 | // properties using the this prefix. |
| 15 | this.getFullTitle = function() { |
| 16 | return this.author + ": " + this.title; |
| 17 | } |
| 18 | |
| 19 | /* |
| 20 | The Storable wrapper handles property access and adds |
| 21 | the following instance methods and properties: |
| 22 | |
| 23 | book.save() - save the instance in the associated store |
| 24 | book.remove() - remove the instance in the associated store |
| 25 | book._type - the object type name - readonly |
| 26 | book._id - the object id - undefined for transient objects, |
| 27 | and only settable on transient objects |
| 28 | */ |
| 29 | return new db.Storable(this, properties); |
| 30 | } |
| 31 | |
| 32 | // init store instance and register persistent classes. |
| 33 | db.store = db.store || new db.Store("db"); |
| 34 | /* |
| 35 | The call to registerType installs the following static |
| 36 | methods in the constructor: |
| 37 | |
| 38 | Book.get(id) - get a persistent object of this type by id |
| 39 | Book.all() - get an array containing all objects of this type |
| 40 | Book.filter() - not yet implemented |
| 41 | */ |
| 42 | db.store.registerType(Book); |
Note: See TracBrowser for help on using the browser.