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

Allow null or empty string in select options #42

Merged
merged 1 commit into from
Jan 3, 2013

Conversation

jr314159
Copy link
Contributor

@jr314159 jr314159 commented Jan 2, 2013

The logic in evaluatePath seems to have a problem where a select option with null or "" is causing the entire object to be assigned to the element's data instead of just the value. This seems to fix things.

@delambo
Copy link
Member

delambo commented Jan 2, 2013

Hi @jr314159 - can you give more details on the bug or a use case because I think what you are describing is correct.

@jr314159
Copy link
Contributor Author

jr314159 commented Jan 2, 2013

Sure. We are using stickit with <select> elements. When we configure stickit to bind to this element, we pass it an array of label, value pairs. We want the first option in our list to have a label but have an empty or null value like so:

<option value="">Choose an option </option>

This was not working with stickit because when we pass the label value pair ['', 'Choose an option'] in our selectConfig, it runs evaluatePath, and the empty string evaluates to false, so it returns the entire javascript object instead of just the value. Then when we did model.get('foo') it would return an object instead of '' as it should have.

@delambo
Copy link
Member

delambo commented Jan 2, 2013

@jr314159, can you give an example of the actual options collection/array you are working with and the selectOptions, please?

@jr314159
Copy link
Contributor Author

jr314159 commented Jan 2, 2013

Here is an example piece of the bindings object we pass into stickit():

{
  '[name="behavior[rule_type]"]': {
    modelAttr: "rule_type",
    selectOptions: {
      collection: function() {
        return [
          {
            label: 'Choose Behavior',
            value: ''
          },
          {
            label: 'Sessions',
            value: 'session'
          }
        ]
      },
      labelPath: "label"
      valuePath: "value"
    }
  }
}

@delambo delambo merged commit ff3e2f7 into nytimes:master Jan 3, 2013
@delambo
Copy link
Member

delambo commented Jan 3, 2013

@jr314159 - thanks for the example. I merged this in a little differently, since really what we are after is preserving non-null values. Get the latest from master and try it on your codebase.

Thanks for reporting.

@jr314159
Copy link
Contributor Author

jr314159 commented Jan 3, 2013

Awesome, thanks.

@gxxcastillo
Copy link
Contributor

What was the fix?

I'm on ver 0.6.3 and I'm having the same problem when I create a <select> with the following:

collection: function () {
     return [
          {"label":"None","value":null}
          , {"label":"6 Months","value":6}
          , {"label":"7 Months","value":7}
          , {"label":"8 Months","value":8}
          ....
     ];
}

In this case, selecting "None" ends up saving the object {label: "None", value: null} on the model.

@gxxcastillo
Copy link
Contributor

Ah, I see it now. Any chance we can get it to save NULL values? I'm serializing my models and persisting them to the server and it would be great to be able to send NULL as a value.

@gxxcastillo
Copy link
Contributor

My workaround for now is to define an onSet function that checks if the value is an object and if so, returns NULL.

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

Successfully merging this pull request may close these issues.

None yet

3 participants