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

Collections

ScottDowne edited this page May 12, 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 collection object

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

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

collections.schema and collections.data

collections.schema and collections.data are objects you can explore to see the existing data and scehma stored in the collections.

collections.schema

collections.schema looks like this:

{
  "schemaname": {
    "fieldname": {
      "fieldproperty": value
    }
  }
}

An example schema set:

{
  "contacts": {
    "address": {
      "type": "Address"
    },
    "name": {
      "type": "Ntring"
    },
    "description": {
      "type": "Text"
    }
  },
  "pets": {
    "owner": {
      "type": "Name"
    },
    "name": {
      "type": "Name"
    }
  }
}

collections.data

collections.data looks like this:

{
  "schemaname": [
    {
      "fieldname": value
    },
    {
      "fieldname": value
    }
  ]
}

An example data set:

{
  "contacts": [
    {
      "address": "123 component st",
      "name": "Component Button",
      "description": "Mr. Button is a model citizen. "
    },
    {
      "address": "123 schema st",
      "name": "Schema Contact",
      "description": "Mr. Contact is a model schema. "
    }
  ]
}

collections.addCollection and collections.removeCollection

To add a collection, call collections.addCollection and to remove one call collections.removeCollection

collections.addCollection(collectionName);

This adds an empty collection under the key collectionName

collections.removeCollection(collectionName);

This removes an existing collection added under the key collectionName

collections.addField and collections.removeField

To add a schema field call collections.addField and to remove a schema field call collections.removeField

collections.addField(collection, name, data);

collection a string for the collection to store this field under.

name a string for the name of the new field.

data an optional object for any properties used for this field.

collections.removeField(collection, name);

collection a string for the collection that this field is under.

name a string for the name of the field to remove.

collections.addItem and collections.removeItem

To push a data item in collections.data use collections.addItem and to remove one, use collections.removeItem

collections.addItem(collection, data);

collection a string for the name to store this data item under.

data an object to store as the data set under collection

collections.removeItem(collection, index);

collection a string for the collection the data lives under.

index the index for the data to be removed.

Saving schemas

A schema is saved when the app itself is saved.

collections.saveData

collections.saveData();

Saves whatever is in collections.data Call this function inside your component, example, a submit button would call this function.

Events

Component authoers can listen for changes in schema or data.

collectionchange

collections.addEventListener("collectionchange", function(e) {
  e.detail.added // Name of new collection
  e.detail.removed // Name of removed collection
});

fieldchange

collections.addEventListener("fieldchange", function(e) {
  e.detail.collection // Collection that has the field being changed
  e.detail.field // Name of the field being changed
  e.detail.added // Added field data
  e.detail.removed // Removed field data
});

Clone this wiki locally