Skip to content

Commit

Permalink
Add support for inline schema spec:
Browse files Browse the repository at this point in the history
withField and withButton now update the schema and buttons objects if they are called as template helpers.
  • Loading branch information
cwohlman committed Aug 30, 2014
1 parent 666ad3b commit 7b43aab
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 3 additions & 1 deletion reactive-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Forms.reactiveContext = function (item, helpers) {

// Convert last helper argument to object
// see blaze/spacebars documentation for why this is necessary
args[args.length - 1] = args[args.length - 1].hash;
if (args[args.length -1] instanceof Spacebars.kw) {
args[args.length - 1] = args[args.length - 1].hash;
}

if (args.length == 1) {
// Only the hash value was passed
Expand Down
48 changes: 47 additions & 1 deletion reactive-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,32 @@ Forms.helpers = {
}
// sub-context helpers
, withField: function (field, fieldName) {

if (typeof field == 'string') {
fieldName = field;
field = this.get('schema', fieldName);
}

if (field instanceof Spacebars.kw) {
field = field.hash;
this.set('schema', field.name, field);
}

if (!fieldName) {
fieldName = field.name;
}

var fieldDefaults = this.field || {};

// field represents a schema entry
return _.defaults({
fieldName: fieldName
, fieldDefaults: fieldDefaults
, field: _.defaults({
name: fieldName
}
, field
, this.field || {}
, fieldDefaults
)
// I think there's no need to allow a field to extend options,
// options are fundementally form level, you should specify
Expand All @@ -93,7 +111,35 @@ Forms.helpers = {
, withFields: function () {
return _.map(this.schema, this.withField, this);
}
, withChild: function (child) {
var field = this.get('field', true);
var schema = field.schema || {};
var fieldDefaults = field.field || {};

return Forms.reactiveContext(child, {
schema: schema
, field: _.defaults({}, fieldDefaults, this.fieldDefaults)
}, this);
}
, withChildren: function () {
// should be called from inside a withField context
return _.map(this.get(this.fieldName) || [], this.withChild, this);
}
, withButton: function (button, buttonName) {
if (typeof button == 'string') {
buttonName = button;
button = this.get('buttons', buttonName);
}

if (button instanceof Spacebars.kw) {
button = button.hash;
this.set('buttons', button.name, button);
}

if (!buttonName) {
buttonName = button.name;
}

// button represents a buttons entry
return _.defaults({
buttonName: buttonName
Expand Down

0 comments on commit 7b43aab

Please sign in to comment.