Library: storage
Overview
Easy way to persist data between requests.
Importing the "storage" module gives you access to a root
storage object. Any property you assign to this object
in one request will be avialable in any subsequent requests.
The typical way to add a top-level property of
storage is to check if it's defined and – if not
– set it to some default value, as in the example below.
source: storage.js
examples
import("storage");
if (! storage.counter) {
storage.counter = 0;
}
Constructors
An object that can be stored directly, and is backed by our
database. StorableObjects can be added to StorableCollections.
parameters
| obj |
properties
- string id
examples
var s = new StorableObject(); s.message = "Hi there!"; storage.foo = s;
var s = new StorableObject({message: "Hi there!"});
var c = new StorableCollection();
c.add(s)
A StorableCollection persistently stores other StorableObjects.
methods
- add(obj)
- remove(obj)
- filter(match)
- sort(compare)
- forEach(f)
- size()
inherits
id() from StorableObject
examples
storage.foo = new StorableCollection();
storage.foo.add({name: "John"});
storage.foo.add({name: "Mary"});
storage.foo.forEach(printp);
Functions
Gets a StorableObject or StorableCollection by id.
parameters
| string | id | The id of the object to be gotten. |
returns
| StorableObject |
The StorableObject whose id is id,
or undefined if no such object exists.
|
examples
function get_editor() {
// Objects passed between requests must be reachable from storage.
var obj = storage.users.iterator().next();
// In your edit page; "obj" is the object to edit.
print(FORM({action: "/doedit", method: "post"},
INPUT({type: "text", name: "title"}),
INPUT({type: "hidden", name: "id", value: obj.id}),
INPUT({type: "submit", name: "edit"})));
}
function post_doedit() {
// In your form's action page.
getStorable(request.param("id")).title = request.param("title");
}
Adds a StorableObject to this collection.
parameters
| object | obj |
The object to add to this collection. If
obj is not a StorableObject, a new StorableObject is
created from obj by passing it to the StorableObject
constructor, copying its properties.
|
examples
var c = new StorableCollection();
c.add({name: "John"});
Removes StorableObjects from this collection.
parameters
| object | obj |
The object to remove from this collection. If
obj is a StorableObject, obj itself is
removed from this collection. If obj is an collection
or a view on a collection (such as one created by filter), then all objects
provided in that collection are removed. Finally, if
obj is just a plain object, then all members of this
collection that have the same properties and values as
obj are removed. Note: passing an {}
removes all objects form this collection.
|
examples
var c = storage.users; // a StorableCollection
c.remove({name: "John"});
Filters this collection based on a matching object.
parameters
| object | match |
The object to base a filter on. The returned
view contains only those members of this StorableCollection whose
properties have the same value as the ones in
match. (Properties in the collection's members not
present in match are ignored.) filter and
sort are "chainable" operations; they can be applied
to filtered and sorted views.
|
returns
| A filtered view of this collection. |
examples
var c = new StorableCollection();
c.add(new StorableObject({first_name: "Bob", last_name: "Smith"}));
c.add(new StorableObject({first_name: "John", last_name: "Smith"}));
var i = c.filter({first_name: "John"});
i.forEach(printp) // prints John Smith's entry above.
// Filter and sort are chainable, as in this example:
var c = storage.users; // a StorableCollection
var view =
c.filter({first_name: "John"}).sort(function(a, b) { return a.age - b.age })
view.forEach(printp) // prints users named John, sorted from youngest to oldest.
Sorts this collection based on a sorting function.
parameters
| function | compare |
As with the function argument to
Array.sort, compare should take two arguments
a and b, and return a negative value, 0, or a
positive value, if a < b, a =
b, or a > b,
respectively. filter and sort are
"chainable" operations; they can be applied to filtered and sorted
views.
|
returns
| A sorted view of this collection. |
examples
c.sort(function(a, b) { return a.date - b.date })
Executes a function once on each member of this collection. Note
that forEach can only be called once on a view (though it can be
called multiple times on a StorableCollection).
parameters
| function | f |
The function to call on each member of this
collection. Returning false will cause forEach to abort.
|
StorableCollection.size()
Returns the number of elements in a collection. Can also be applied
to filtered and sorted views of a collection. (This number may be
approximate if your collection is very large.)
returns
| number | The size of this collection. |
examples
c.filter({status: 3}).size()
Objects
string
The id of a StorableObject is auto-generated. You can get the
object associated with a given id using
getStorable.