Feedback wanted: generic field:focus/change/blur and field:render/show events #147

Open
wants to merge 3 commits into
from

Conversation

Projects
None yet
6 participants
Contributor

drd commented Oct 15, 2012

The problem:

I want to be able to update the entire form when any individual field is focused/blurred/changed and also to be able to configure and register event handlers on individual fields as they are rendered

The solution:

I've added generic field:focus, field:change, and field:blur events which get triggered when any editor is focused, changed or blurred. Also, I've added field:render and field:show. I've added tests for these events.

The quandary:

Unlike existing bbf events which bubble the editor, these events pass the field as the second argument to handlers. I don't like this inconsistency, but editors don't have a reference to the field and I needed to style the field in response to these events. There are a few solutions:

  • add a field reference to editors
  • add the editor as the 2nd argument, and the field as a third argument
  • or i can just look up the field in the form based on the name, and have these events have the same signature as the others
Collaborator

philfreo commented Oct 15, 2012

In many of the custom editors that I've written I've wanted access to the field - seems like providing that in a general purpose way would be a good solution.

Contributor

drd commented Oct 15, 2012

rebased to master

Owner

powmedia commented Nov 22, 2012

I've created another issue about changing the arguments passed into event callbacks which could help sort the inconsistencies:

#158

Is it still considered? It would really help me to DRY my views.
I would like to revalidate fields on blur without the need to list them all inside listenTo call.

@listenTo formView, 'name:blur email:blur client:blur', (form, editor) ->
        formView.fields[editor.key].validate()

It creates unnecessary overhead to keep that events list in sync with the schema that defines validators. (e.g. I can add validator to a new field, but forget to add event for that field in that list)

Contributor

fonji commented Oct 20, 2014

+1. I had to do something similar.

Collaborator

exussum12 commented Oct 20, 2014

I would like to revalidate fields on blur without the need to list them all inside listenTo call

I do this by listening to all and filtering blur events (check if it ends with :blur)

Owner

powmedia commented Oct 20, 2014

If anyone is up for updating this PR off the latest code in master I'll merge this in

Collaborator

exussum12 commented Oct 20, 2014

  form.on('all', function(type, form, edited) {
      if (type.endsWith(":blur")) {
          edited.validate()
     }

where endsWith is defined as

   String.prototype.endsWith = function(suffix) {
      return this.indexOf(suffix, this.length - suffix.length) !== -1;
   };

It seems strange to trigger 2 events which are essentially the same

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