nytimes / backbone.stickit

Backbone data binding, model binding plugin. The real logic-less templates.
MIT License
1.64k stars 176 forks source link

Problems with defaultOption as a function, returning false #303

Open nim-ohtar opened 8 years ago

nim-ohtar commented 8 years ago

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