21

I have an input field, which has two event handlers bound to it.

Validate & AutoSave

Obviously I want to validate before I save. If validation fails, the "invalid" class is added to the input and autosave will check for that class before it proceeds.

This works well enough, but is there a way to guarantee Validate runs before Autosave in all cases?

Wayne
  • 59,728
  • 15
  • 131
  • 126
Justin Alexander
  • 2,004
  • 3
  • 21
  • 25

4 Answers4

14

If you use JQuery to bind your events, it guarantees that handlers are fired in the same order that they were bound. Otherwise the order is officially undefined.

If you cannot use JQuery or a similar framework you can easily simulate this by using your own custom even binding, where your generic handler is a function which keeps an array of functions and calls them in order.

Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
9

Normally you'd have the Save event handler call Validate() which will return true if everything is fine and ready to be saved.

function onSaved() {
  if (!validate()) {
    // set class
    return;
  }

  // do the save
}
JacobE
  • 8,021
  • 7
  • 40
  • 48
2

Why not attach just one handler -- Validate -- and call AutoSave from inside it?

For an answer to your question that isn't also a question, see this post or this one or this one.

Community
  • 1
  • 1
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • 6
    because they represent separate modules, which may or may not occur together. Writing them as a single function (which is obvious) has the effect of over coupling the code. – Justin Alexander Feb 28 '11 at 15:33
  • 2
    I disagree. They *are* coupled, as evidenced by the fact that you want one to occur only after the other runs. Putting them together in one function doesn't *create* that coupling. It acknowledges it. (Secondly, just because you call them both inside some function doesn't mean that they have to *always* be called in that way. They can each still be written in a way that preserves their independence.) – Wayne Dec 15 '16 at 22:32
  • I know this is forever ago but to add to this, you could rename Validate to ValidateAndAutoSave to better imply its intended use because it looks like validation implies autosaving but autosaving can happen independently with no implied association. – Austin Schmidt Mar 01 '17 at 15:05
2

Already answered - but just to add this piece of knowledge, the order of event handlers can not be relied upon. It may in any given implementation be predictable, but this can change from one (Javascript) implementation to the next and/or over time. The only thing certain is that they all will be executed - but not in what order.

Note that the situation is similar when there is an event handler for a DOM object and another one for the same event for a child or parent - which of those is executed first is not always clear as well. See http://www.quirksmode.org/js/events_order.html

Mörre
  • 5,699
  • 6
  • 38
  • 63