Skip to content

Latest commit

 

History

History
127 lines (108 loc) · 3.45 KB

cj-dom.asciidoc

File metadata and controls

127 lines (108 loc) · 3.45 KB

Collection+JSON Document Object Model

Note

This is an experimental document. Nothing is settled. Just ideas. "Nothing to see here, move along now." =8^)

Summary

The idea is to create a Document Object Model that makes is easy to program with Cj representations. The point is not to change the Cj message format to look like programming code (e.g. bare JSON). Instead, the point is to treat Cj representations the way most treat HTML representations - as a document model that deserves a programming model, too. Most of the design in this Javascript model below is meant to look like the HTML DOM and offer familiar constructs to JS developers.

This is all very early design work. Looking for feedback and comments that will make this programming model handy, easy to use, and - in the process - make it easier and more enjoyable to work with Cj on both the clients and servers.

Object Model

  // initialize object
  cj = new cjDOM.load(href|obj);
  cj.document // returns {collection:{...}}

  // root properties
  cj.document.href;
  cj.document.version;

  // top nodes
  cj.document.nodes; // returns [links:{},items:{},queries:{},template:{},error:{}]
  cj.document.node('links|items|queries|template|error'); // returns [] or {}
  cj.document.links(); // returns [...]
  cj.document.items(): // returns [...]
  cj.document.queries(); // returns [...]
  cj.document.error(); //returns {...}
  cj.document.template(); // returns {...}
  cj.document.nodes.addChild(obj); // add links, items, queries, error, template, or extension object
  cj.document.nodes.removeChild('name');

  // links
  links = cj.document.links;
  cj.document.links.addChild(obj);
  cj.document.links.removeChild('name');

  links.count;
  links[0]; // returns link object
  l = links['profile']; // returns link object based on link.rel
  l.rel;
  l.href;
  l.prompt;
  l.properties; // returns array of properties
  l.properties[0]; // returns property value based in index
  l.properties['href']; // returns single property value based on name
  l.properties.addChild(obj|string);
  l.properties.removeChild('name');

  // items
  items = cj.document.items;
  cj.document.items.addChild(obj);
  cj.document.items.removeChild('name');

  items.count;
  items[0];
  i = items['http://...'];
  i.prompt;
  i.name;
  i.value;
  i.render;
  i.properties;
  i.properties[0];
  i.properties['render'];
  i.properties.addChild(obj|string);
  i.properties.removeChild('name');
  data = i.data; // returns array of data
  links = i.links; // returns array of links

  // queries
  queries = cj.document.queries;
  cj.document.queries.addChild(obj);
  cj.document.queries.removeChild('name');

  queries.count;
  queries[0];
  q = queries['avatar']; // based on rel property
  q.rel;
  q.prompt;
  q.href;
  data = q.data;

  // template
  t = cj.document.template;
  t.href;
  data = t.data;

  // error
  e = cj.document.error;
  e.title;
  e.code;
  e.message;
  e.properties;
  e.properties[0];
  e.properties['message'];

  // data
  data = cj.document.queries[0].data;
  data = cj.document.template.data;
  data = cj.document.items[0].data;

  data.count;
  data[0];
  d = data['firstName']; // based on name property
  d.name;
  d.value;
  d.prompt;
  d.properties;
  d.properties[0];
  d.properties['name'];
  d.properties.addChild(obj|string);
  d.properties.removeChild('name');