Skip to content

pass binding context through to custom binding functions #37

@elainemacurdy

Description

@elainemacurdy

This is a follow up to the solution implemented for #22.

Any chance we could get the binding context passed through to the custom binding function too? I need to do something like the following:

var bindingToggleFormField = function(el, value, previousValue, bindingContext) {
  var fieldName = el.getAttribute('data-hook');
  if (this._fieldViews[fieldName]) {
    var newValue = value && bindingContext.showWhen;
    this._fieldViews[fieldName].set('isVisible', newValue);
  }
}
...
bindings: {
  isFoo: {
    type: bindingToggleFormField,
    hook: 'barField',
    showWhen: [true || false]
  }
}

props: {
  isFoo: 'boolean'
}

FWIW in the short term I implemented this kind of thing:

var bindingToggleFormField = {
  showWhenTrue: function(el, value, previousValue) {
    var fieldName = el.getAttribute('data-hook');
    if (this._fieldViews[fieldName]) {
      this._fieldViews[fieldName].set('isVisible', value);
    }
  },
  showWhenFalse: function(el, value, previousValue) {
    var fieldName = el.getAttribute('data-hook');
    if (this._fieldViews[fieldName]) {
      this._fieldViews[fieldName].set('isVisible', !value);
    }
  }
}
...
bindings: {
  isFoo: {
    type: bindingToggleFormField.showWhenTrue || bindingToggleFormField.showWhenFalse,
    hook: 'barField'
  }
}

... which gets the job done, but is less elegant.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions