HTML and CSS Reference
In-Depth Information
15.5.2.1 Refactoring: Extracting the Common Parts
We will take a small detour by refactoring the user form controller. We will ex-
tract a formController object from which both of the controllers can in-
herit. Step one is adding the new object, as Listing 15.66 shows. Save it in src/
form _ controller.js .
Listing 15.66 Extracting a form controller
(function () {
if (typeof tddjs == "undefined") {
return;
}
var dom = tddjs.dom;
var chat = tddjs.namespace("chat");
||
||
if (!dom
!dom.addEventHandler
!Function.prototype.bind) {
return;
}
function setView(element) {
element.className = "js-chat";
var handler = this.handleSubmit.bind(this);
dom.addEventHandler(element, "submit", handler);
this.view = element;
}
chat.formController = {
setView: setView
};
}());
To build this file, I simply copied the entire user form controller and stripped
out anything not related to setting the view. At this point, you're probably wondering
“where are the tests?”. It's a valid question. However, we are not adding or modifying
behavior, we're merely moving around parts of the implementation. The existing
tests should suffice in telling us if the refactoring is successful—at least for the
documented/tested behavior, which is the only behavior we're concerned about at
this point.
Step two is making the user form controller use the new generic controller. We
can achieve this by popping it in as the form controller's prototype object, as seen
in Listing 15.67.
 
Search WWH ::




Custom Search