Skip to content
This repository was archived by the owner on May 2, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

build: components index.js forms.css template.js
build: components index.js forms.css
@component build --dev

template.js: template.html
@component convert $<

components: component.json
@component install --dev

Expand Down
5 changes: 3 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

## API

### var form = new Form(schema);
Creates new Form object based on schema object
### var form = new Form(schema, [templates]);
Creates new Form object based on schema object.
If ``templates`` is defined, it overrides templates.js.

### form.render()
Renders form
Expand Down
14 changes: 9 additions & 5 deletions attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
var Emitter = require('emitter')
, event = require('event')
, domify = require('domify')
, templates = require('./template')
, type = require('type')
, minstache = require('minstache')
, val = require('val');
Expand All @@ -24,6 +23,11 @@ module.exports = Attribute;
var safe = ['options', 'type', 'title', 'repeat', 'properties'];


/**
* make templates global available
*/

var templates;

/**
* Initialize a new `Attribute` with a `name` and
Expand All @@ -34,10 +38,10 @@ var safe = ['options', 'type', 'title', 'repeat', 'properties'];
* @api public
*/

function Attribute(name, schema) {
function Attribute(name, schema, _templates) {
if (!name) throw Error('No name provided');
if (!schema) throw Error('No parameters provided');

templates = _templates;
for (var opt in schema) {
if (safe.indexOf(opt) == -1) continue;
this[opt] = schema[opt];
Expand Down Expand Up @@ -166,7 +170,7 @@ Attribute.prototype.object = function() {
for (var property in this.properties) {
var subParams = this.properties[property]
, subName = this.name + '.' + property
, subAttribute = new Attribute(subName, subParams);
, subAttribute = new Attribute(subName, subParams, templates);

this.node.appendChild(subAttribute.render().view);
this.attributes[property] = subAttribute;
Expand All @@ -187,7 +191,7 @@ Attribute.prototype.object = function() {

Attribute.prototype.repeatAttribute = function(){
var name = this.name + this.repeatCount;
var attribute = new Attribute(name, this);
var attribute = new Attribute(name, this, templates);
attribute.repeat = false;
return attribute.render();
}
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var Attribute = require('./attribute')
, templates = require('./template')
, Emitter = require('emitter');


Expand All @@ -20,8 +21,9 @@ module.exports = Form;
* @api public
*/

function Form(schema) {
function Form(schema, _templates) {
if (!schema) throw Error('No schema provided');
if (_templates) templates = _templates;
this.schema = schema;
}

Expand All @@ -48,7 +50,7 @@ Form.prototype.render = function() {

for (var name in this.schema) {
var subSchema = this.schema[name]
, attribute = new Attribute(name, subSchema);
, attribute = new Attribute(name, subSchema, templates);

this.view.appendChild(attribute.render().view);
this.attributes[name] = attribute;
Expand Down