0

I'm building customisable form in bacon.js. I have a problem with optional fields that can be empty e.g. phone is sometimes required or sometimes optional.

field_events = _.merge(
  _.mapValues(@form, @_build_field_streams), dynamic_field_events
)
valid_streams = _.pluck(_.values(field_events), 0)
all_invalid = Bacon.all(valid_streams).not().toProperty()

data_prop = Bacon.combineTemplate(
  _.mapValues(field_events, ([is_valid, value_stream]) -> value_stream)
)

submit_stream = @$el
  .asEventStream('click', @submit_selector)
  .doAction(".preventDefault")
  .alwaysSkipWhile(all_invalid)

@actions = Bacon.when(
  [data_prop, submit_stream],
  (data) -> {action: 'submit', param: data}
)

data_prop is evaluated lazily, so if phone stream doesn't have any value, we won't submit the form. Is there a way to give stream a default value or filter empty streams from combineTemplate ?

Rest of the code:

_build_field_streams: ({selector, validator}, field) =>
value_stream = Bacon.mergeAll(
  @_get_field_change_streams(selector)
).map((e) ->
  $(e.currentTarget).val()
)

if _.isString validator
  validator = exports.validators[validator]()

# optional field are valid by default and for empty values
is_optional_field = field not in @mandatory_field
curr_validator = validator || @noop_validator
validator_fun = (x) -> if is_optional_field and not x
  return true
else
  return curr_validator(x)
validator_stream = value_stream.map(validator_fun)

is_valid = validator_stream.map(_.isEmpty).toProperty(is_optional_field).log('field')

[ok_events, bad_events] = validator_stream.split(_.isEmpty)
bad_events = bad_events.debounce(@invalid_delay)

defocus = @$el.asEventStream('blur', selector)

# TODO: Might be good to instantly go bad _if_ it was already valid.
Bacon.when(
  [ok_events],            true
  [is_valid, bad_events], ((valid) -> valid),
  [is_valid, defocus],    ((valid) -> valid)
).onValue( (valid) =>
  if valid
    @render_field_valid(selector)
  else
    @render_field_invalid(selector)
)
return [is_valid, value_stream]

_get_field_change_streams: (selector) ->
  # Hopefully these 2 cover most things.  Can always add more if we need.
  return _.map ['change', 'keyup'], (handler) =>
    @$el.asEventStream(handler, selector)
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108

1 Answers1

0

With current code I had to type something in every input. My understanding is Bacon.when( [data_prop, submit_stream] will fire if data_prop is non-empty Property and we receive submit_stream event. Why is data_prop not evaluated? I believe Bacon.combineTemplate won't create a property with value unless all value_stream had an event.

I converted stream to a prop with a default value

data_prop = Bacon.combineTemplate(
  _.mapValues(field_events, ([is_valid, value_stream]) -> value_stream.toProperty(''))
)
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108
  • Did this solve your problem? I'm still not quite sure what your exact problem is here :) – raimohanska Jan 26 '16 at 07:31
  • My understanding of bacon.js is limited but I updated the answer. – Lukasz Madon Jan 26 '16 at 13:02
  • 1
    So the problem was that some the Properties representing the values of your inputs didn't have a value (because there was no input event) and thus combineTemplate obviously didn't have a value either. You added a default value to the Properties and now it works. Problem solved? – raimohanska Jan 27 '16 at 13:50