var $form = $(this)
Sure; that code is not at all obvious.
When an event occurs and we've attached a handler function, we know that the browser creates an event object that stores a bunch of information about the event.
There's event an event.target
which is the DOM
element that the event happened to, such as the button
that was clicked or the form that was submitted.
The browser also binds the
keyword this
to the event.target
.
So, the code above makes the $form
be a jQuery
wrapped set consisting of the form that was submitted.
Later code will use that to serialize the form.
The general notion of serializing, which is used throughout CS, is the idea of converting a data structure or other complex, non-linear thing, into a linear (serial) thing. Typically textual.
Consider converting an array of strings and integers and such into a string.
Now, in CoffeeRun, serializing to a string is annoying and inconvenient. Instead, we want to serialize into a JS Object Literal (a dictionary).
The keys of the dictionary will be the names of the inputs: kind
, size
, toppings
The values of the dictionary will be the user's inputs: veggie
, personal
, pepperoni
In general, a callback is a function that is invoked when some event occurs or when some data is encountered.
I'll describe a metaphor that I hope may help.
For the FormHandler
class, the callback is what we (the client)
want to happen when the form is submitted.
See client code
The callback does the specific stuff, for the submission
of this particular form, while the FormHandler
method does the generic stuff that is done for all forms.
The general method invokes the callback when it is ready to do the specific stuff.