Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Collections

ScottDowne edited this page Jul 16, 2014 · 15 revisions

Collections

A shared data storage that components can use. Example: A component that creates and stores form data, can store it in the collections so another component can see or use the same data. It'll save it, and persist between app loads.

The Collections Object

var collections = document.querySelector("ceci-collections");

collections Stores and manages all the collections available to this app. You can call collections related functions from it. You can access this and use it from inside your component.

Collection Functions

collections.addCollection(name)

collections.removeCollection(name)

collections.getCollection(name)

collections.getCollections()

collections.getField(collectionname, fieldname)

collections.getFields(name)

Collections Events

Listen for changes in collections or data. You can add events using collections.addEventListener(event, function(detail) {})

collectionchange

detail.added detail.removed

Collection

The Collection Object

var collection = collection.getCollection()[0];

collection Is a single collection. You can call collection related functions from it. You can access this and use it from inside your component.

Collection Functions

collection.addField(name)

collection.removeField(name)

collection.getField(name)

collection.getFields()

collection.name

collection.addItem(item)

collection.removeItem(index)

collection.updateItem(index, item)

collection.saveData()

Collection Events

Listen for changes in collection or data. You can add events using collection.addEventListener(event, function(detail) {})

itemchange

detail.added detail.removed detail.index

fieldchange

detail.added detail.removed

propertychange

detail.changed

Fields

The Field Object

var field = collection.getFields()[0];

field is a thing you can call field related functions from. You can access this and use it from inside your component.

Field Functions

field.kind

field.name

field.getCollectionName()

Field Events

propertychange

detail.changed

What Simon Hopes It'll Look Like:

var collections = document.querySelector("ceci-collection");

collections.addCollection('things');

collecitons.things.addField('name', collections.kinds.Text);
collecitons.things.addField('value', collections.kinds.Number);

collections.things.fields 
=> { // dunno if this is right
    name: collections.kinds.Text
    value: collections.kinds.Number
  }
]

collections.things // returns localStorage['ceci-collection-things']
=> [
]

collections.things.push({
  name: "Fart Machine",
  value: Infinity
});

collections.things
=> [
  {name: "Fart Machine", value: Infinity}
]

Note from david

Experience w/ FFOS has shown that synchronous calls to localStorage are a performance Bad Idea™. I would suggest using http://mozilla.github.io/localForage/ instead as the backend API, as that will force async. This will have implications for the API that @simonwex asks for.

Some apps are usable within the designer, and in particular data can be populated within both the component properties editor and using the apps themselves. I think we should think a bit about whether/how the component author and app author can manage that workflow. A first draft would be to ask, on publishing, which of the collections the app developer wants to "ship with the app", and have the app serialization/deserialization code take that data and stuff it in phone-side storage on first run.