Contents
About
Help
StorableCollections
Please do not save test edits. If you want to experiment, please use the
sandbox.
StorableCollections provide a way to organize a set of similar StorableObjects. (If you are familiar with Relational Databases such as MySQL, a StorableCollection is similar to a "table"). *Here's a simple app that stores a list of awesome people.* import("storage"); if (!storage.awesomePeople) { storage.awesomePeople = new StorableCollection(); var ap = storage.awesomePeople; ap.add({name: "Arnold Schwarzenegger", title: "Governator"}); ap.add({name: "Larry David", title: "Old Jewish Comedian"}); ap.add({name: "Janet Jackson", title: "Rock Star"}); } storage.awesomePeople.forEach(function(person) { print(P(person.name, " (", person.title, ")")); }); Note that we used the add function to put objects into the StorableCollection. Once there are objects in a StorableCollection, you can use various functions to access particular ones in particular orders. These functions can be "chained" together however you like, as in the following example. import("storage"); if (!storage.visitors) { storage.visitors = new StorableCollection(); } // Add the current visitor to the collection. storage.visitors.add({ip: request.clientAddr, when: new Date()}); // Display recent visitors print(P("Recent visitors to this page:")); var t = TABLE({border:1}); storage.visitors.sortBy("-when").limit(10).forEach(function(v) { t.push(TR(TD(v.ip), TD(v.when))); }); print(t); ###*Further Notes:* * Unlike most relational databases, there is no pre-defined schema for StorableCollections. You can add new properties on the fly to any object in a collection and it will just work. * For debugging, you can print a StorableCollection directly with print(storage.myCollection). This will display a formatted table. * Each object in a StorableCollection is a StorableObject, and therefore has a unique id field, which can be used with getStorable() to retrieve it directly. * See the reference docs for StorableCollections.