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

Setting attribute value to undefined through bindingFilters #139

Open
jekuno opened this issue May 20, 2016 · 1 comment
Open

Setting attribute value to undefined through bindingFilters #139

jekuno opened this issue May 20, 2016 · 1 comment

Comments

@jekuno
Copy link

jekuno commented May 20, 2016

The current implementation of the integer bindingFilter returns 0 for empty strings which is ok.
see: https://github.com/gmac/backbone.epoxy/blob/master/backbone.epoxy.js#L962

I want to add a bindingFilter which returns undefined for empty input fields (i.e. empty strings). It looks like this (CoffeeScript):

bindingFilters:
  integerOrUndefined:
    get: (value) ->
      if value then parseInt(value, 10) else undefined

     set: ( value ) ->
      if value then parseInt(value, 10) else undefined

If I use this bindingFilter and the user enters integers to the input fields the attribute value gets set correctly. If the user clears the input field the set function also correctly gets triggered and returns undefined as expected. However the according attribute never gets set to undefined.

I think this issue is due to a special handling of undefined values in backbone.epoxy. Is there any workaround for this?

@jekuno
Copy link
Author

jekuno commented Jun 9, 2016

I found the reason for this bug. The isUndefined check in line https://github.com/gmac/backbone.epoxy/blob/v1.3.1/backbone.epoxy.js#L870 prevents undefined values from being set. (Also because of this no change events are sent when changing a value to undefined using the set function of a bindingFilter).

I currently use the following workaround:

bindingFilters:
    integerOrUndefined:
      get: (value) ->
        value

      set: ( value ) ->
          parseInt(stringValue, 10)

The set function of the above workound returns NaN for empty strings or strings which cannot be passed using parseInt.
The getter returns this NaN value to the view which gets displayed as an empty form field in turn (if needed you can also convert NaN values to undefined in the get function).

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

No branches or pull requests

1 participant