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

Dynamic options inside object arrays #585

Closed
craig-l opened this issue Jan 2, 2015 · 6 comments
Closed

Dynamic options inside object arrays #585

craig-l opened this issue Jan 2, 2015 · 6 comments

Comments

@craig-l
Copy link

craig-l commented Jan 2, 2015

Related to #553, except we are doing this inside an array.
We're generating dynamic options inside autoform.options as follows

color: {
    type: String,
    optional: true,
    label: 'Color',
    autoform: {
        options: function() {
            var model = AutoForm.getRelativeFieldValue('selectedCars.$.model');
            return Cars.findOne(model).colors.map(function (color) {
                return {
                    label: color.name,  //.name is human readable
                    value: color.dbName //.dbName has spaces stripped and camel cased
                }
            });
            }
    }
}

This works and the select options are reactive, even when inside a deeply embedded array and looking somewhere other than a sibling.
Related to #584.. this.field('selectedCars.$.model') would be helpful/more slick

We made getRelativeFieldValue like so

AutoForm.getRelativeFieldName = function getRelativeFieldName (field) {
    var thisField = Template.parentData(1);
    if (!thisField || !thisField.name) {
        return field;
    }
    var source = thisField.name.split('.');
    var target = field.split('.')
    var name = _.map(target, function (token, index) {
        if (token === '$') {
            return source[index] || token;
        }
        return token;
    }).join('.');
    return name;
}

AutoForm.getRelativeFieldValue = function getRelativeFieldValue (field) {
    var value = AutoForm.getFieldValue(AutoForm.find().formId, AutoForm.getRelativeFieldName(field));
    return value;
}
@craig-l craig-l changed the title Dynamic Options Dynamic options inside object arrays Jan 3, 2015
@aldeed
Copy link
Collaborator

aldeed commented Jan 27, 2015

I think something like this can be added, but still need to think a bit on how best to do it.

@BradRyan
Copy link

BradRyan commented Feb 7, 2015

+1 on reactive way to grab other field values in an array of objects and dynamically set autoform.options. Thanks @craig-l for the suggestion. I'll give it a shot.

@BradRyan
Copy link

BradRyan commented Feb 8, 2015

This worked for trying to set options, but not when trying to use the getRelativeFieldValue function in an autoValue function. Looks like I might want to try a custom template like noted on #202... would be nice to keep all the settings in the schema.

@BradRyan
Copy link

BradRyan commented Feb 8, 2015

I just tried, but no luck. I actually don't see my autovalue function getting called at all. Hmmm

~Brad

On Feb 7, 2015, at 9:54 PM, Craig L notifications@github.com wrote:

Did you try
var field = AutoForm.getRelativeFieldName('xx.$.xx');
var value = this.field(field).value


Reply to this email directly or view it on GitHub.

@BradRyan
Copy link

BradRyan commented Feb 8, 2015

Ah it's waiting for an insert/update I believe. I'm looking to try and have default values populate as you fill the form, but can still change them. Maybe autovalue is the wrong place to do this.

~Brad

On Feb 7, 2015, at 9:54 PM, Craig L notifications@github.com wrote:

Did you try
var field = AutoForm.getRelativeFieldName('xx.$.xx');
var value = this.field(field).value


Reply to this email directly or view it on GitHub.

@aldeed
Copy link
Collaborator

aldeed commented Mar 8, 2015

In autoform 5.0, this.name is set within the options function, so you should be able to do

var model = AutoForm.getFieldValue(this.name.replace('.color', '.model'));

We can continue to think of how this might be made easier, but it's a bit better anyway.

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

4 participants