PolymerElements / iron-form

Custom form element
https://www.webcomponents.org/element/PolymerElements/iron-form
63 stars 81 forks source link

Writing a decorator for <iron-form>: a timing question #259

Open shipagency opened 6 years ago

shipagency commented 6 years ago

I am writing a Polymer 2 element that decorated <iron-form> My element needs to work with <iron-form> and the form itself that is decorated by <iron-form> -- which is stored as _form in the <iron-form> element.

At the moment I have this:

ready () {
  super.ready()
  var self = this

  this.ironForm = this.allChildrenNamed('IRON-FORM', true)
  if (!this.ironForm) throw new Error('hot-form must contain an iron-form element which will be decorated')

  setTimeout(function () {
    self.form = self.ironForm._form
  }, 0)
}

Is this a reliable way to go about this? WIth that setTimeout, <iron-form> has enough time to assign _form. Will this be the case longer-term? @TimvdLippe commeted here: https://github.com/Polymer/polymer/issues/5149

There might be a case where iron-form is not attached yet, as the assignment to _form is in that callback (https://github.com/PolymerElements/iron-form/blob/dc9c9635610d857c6cccefdf063d14115274baa6/iron-form.html#L172-L204) I do not think setTimeout is 100% reliable, but it will probably work in most cases.

Since this is mostly an iron-form specific question, I would advise to open an issue there and potentially request a new event to be notified when _form is set, in the attached callback.

Would you accept a PR with the addition of the newly emitted event? Or, what would you advise?