Skip to content

Commit 7b43aab

Browse files
committed
Add support for inline schema spec:
withField and withButton now update the schema and buttons objects if they are called as template helpers.
1 parent 666ad3b commit 7b43aab

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

reactive-context.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Forms.reactiveContext = function (item, helpers) {
55

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

1012
if (args.length == 1) {
1113
// Only the hash value was passed

reactive-form.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,32 @@ Forms.helpers = {
6969
}
7070
// sub-context helpers
7171
, withField: function (field, fieldName) {
72+
73+
if (typeof field == 'string') {
74+
fieldName = field;
75+
field = this.get('schema', fieldName);
76+
}
77+
78+
if (field instanceof Spacebars.kw) {
79+
field = field.hash;
80+
this.set('schema', field.name, field);
81+
}
82+
83+
if (!fieldName) {
84+
fieldName = field.name;
85+
}
86+
87+
var fieldDefaults = this.field || {};
88+
7289
// field represents a schema entry
7390
return _.defaults({
7491
fieldName: fieldName
92+
, fieldDefaults: fieldDefaults
7593
, field: _.defaults({
7694
name: fieldName
7795
}
7896
, field
79-
, this.field || {}
97+
, fieldDefaults
8098
)
8199
// I think there's no need to allow a field to extend options,
82100
// options are fundementally form level, you should specify
@@ -93,7 +111,35 @@ Forms.helpers = {
93111
, withFields: function () {
94112
return _.map(this.schema, this.withField, this);
95113
}
114+
, withChild: function (child) {
115+
var field = this.get('field', true);
116+
var schema = field.schema || {};
117+
var fieldDefaults = field.field || {};
118+
119+
return Forms.reactiveContext(child, {
120+
schema: schema
121+
, field: _.defaults({}, fieldDefaults, this.fieldDefaults)
122+
}, this);
123+
}
124+
, withChildren: function () {
125+
// should be called from inside a withField context
126+
return _.map(this.get(this.fieldName) || [], this.withChild, this);
127+
}
96128
, withButton: function (button, buttonName) {
129+
if (typeof button == 'string') {
130+
buttonName = button;
131+
button = this.get('buttons', buttonName);
132+
}
133+
134+
if (button instanceof Spacebars.kw) {
135+
button = button.hash;
136+
this.set('buttons', button.name, button);
137+
}
138+
139+
if (!buttonName) {
140+
buttonName = button.name;
141+
}
142+
97143
// button represents a buttons entry
98144
return _.defaults({
99145
buttonName: buttonName

0 commit comments

Comments
 (0)