Skip to content

Problems with defaultOption as a function, returning false #303

@nim-ohtar

Description

@nim-ohtar

Hello there,

I am having some troule with slectOptions, more precisely with defaultOption.
I am having the following scenario, there is a select which should have defaultOption in some cases and in others not. I am using the defaultOption property as a function to determine the correct default value.
Here is an example:

defaultOption: function(){
                    var selectedValue = this.model.get("value");

                    if (!selectedValue){
                        return {
                            label: "Veuillez sélectionner une valeur",
                            value: ""
                        };
                    } else {
                        return false;
                    }
                }

Problem is default option is still added, as if

defaultOption: { label: "", value: "" }

is applied. I could not find appropriate workaround for this, so I checked the code.
The code in question is the following:

 if (selectConfig.defaultOption) {
    var option = _.isFunction(selectConfig.defaultOption) ?
      selectConfig.defaultOption.call(this, $el, options) :
      selectConfig.defaultOption;
     addSelectOptions(["__default__"], $el, option);
  }

My suggestion is this change:

  if (selectConfig.defaultOption) {
    var option = _.isFunction(selectConfig.defaultOption) ?
      selectConfig.defaultOption.call(this, $el, options) :
      selectConfig.defaultOption;
      //changed code
      if (option) {
          addSelectOptions(["__default__"], $el, option);
      }
  }

If you guys, can help me out with an appropriate workaround or feedback for the proposed change I would appreciate it.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions