Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to unravel the mystery of field binding #144

Open
rbarkas opened this issue Oct 3, 2014 · 1 comment
Open

Trying to unravel the mystery of field binding #144

rbarkas opened this issue Oct 3, 2014 · 1 comment

Comments

@rbarkas
Copy link

rbarkas commented Oct 3, 2014

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' });
@ljharb
Copy link
Collaborator

ljharb commented Oct 3, 2014

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants