Trying to unravel the mystery of field binding
I'm dynamically creating a form based on info loaded from a json file. I've tried everything I can think of to get a value loaded from the json to display in a widget but nothing I've tried works. The documentation regarding binding is (in my opinion) a bit opaque. (forgive me if binding is not the right functionality - it just seemed to be what I was looking for)
This is the last obstacle I have to overcome before using forms in production. Could someone show me the error of my ways? Thanks in advance.
To summarize: As I build the form, I have a value that I want to insert into the form input element that will be visible when the page is rendered.
/* configObject is an object parsed from json I load from a file
each itemElement below contains:
{
"value": <a string>,
"type": <a string - 'integer', 'float', 'string', or 'boolean'>,
"length": <an integer>
}
i.e.
fieldName1: {
"value": "contentOfFieldName1",
"type": "string",
"length": 12
}
*/
_.each(configObject, function (itemElement, itemIndex) {
if (['integer', 'float'].indexOf(itemElement['type']) >=0) {
fieldType = 'number'
} else {
fieldType = itemElement['type'];
}
/*
At this point, itemElement.value = the value I want to insert as the visible value of the form field.
How do I do this?
I've tried newField.bind(itemElement.value);
*/
var newField = fields[fieldType]({required: true});
//newField.bind(itemElement.value); // no error but nothing happens
formObject[itemIndex] = newField;
//formObject[itemIndex].bind(itemElement.value); // no error but nothing happens
//formObject.bind({itemIndex: itemElement.value}); // error: formObject has no method 'bind' });
If you refer to the tests, you'll see that field.bind('foo') will bind 'foo' as the value, which will then show up when rendering the field.
If that helps, and you would like to submit a PR improving the bind documentation, I'd be happy to accept it!