diff --git a/Makefile b/Makefile index 7f7ad64..eab00cd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Readme.md b/Readme.md index 3fc6608..441181a 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/attribute.js b/attribute.js index 5512c4f..2ac5279 100644 --- a/attribute.js +++ b/attribute.js @@ -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'); @@ -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 @@ -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]; @@ -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; @@ -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(); } diff --git a/index.js b/index.js index 10a31b9..bb41030 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ */ var Attribute = require('./attribute') + , templates = require('./template') , Emitter = require('emitter'); @@ -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; } @@ -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;