Table of Contents
In this chapter, we show how to build a front-end web application with enumeration attributes, using plain JavaScript. We also show how to deal with multi-valued attributes because in many cases, enumeration attributes are multi-valued.
Compared to the Validation App discussed in Chapter 5, we now deal with the following new issues:
We replace the ES5 constructor-function-based class definition
of our model class Book
with a corresponding ES6
class
definition, which provides a more convenient syntax
while preserving the prototype-based semantics of JS constructor
functions.
Instead of defining explicit setters we now make use of implicit
get
/set
methods for properties because they
can be conveniently defined within a class
definition.
Enumeration datatypes have to be defined in a suitable way as part of the model code.
Enumeration attributes have to be defined in model classes and handled in the user interface with the help of suitable choice widgets.
In terms of coding, the new issues are:
In the model code we now have to take care of
defining an ES6 class
for
Book
;
replacing the constructor parameter slots
with a single record parameter using the ES6 syntax for function
parameter destructuring.
defining get
/set
methods for
all properties of the Book
class
definition;
defining the enumerations with the help
of a utility class Enumeration
, which is
discussed below;
defining the single-valued enumeration
attributes originalLanguage
and category
together with their check functions
checkOriginalLanguage
and
checkCategory
;
defining the multi-valued enumeration
attributes
otherAvailableLanguages
and
publicationForms
together with their check
functions checkOtherAvailableLanguages
and
checkPublicationForms
;
extending the methods Book.update
and
Book
::toString
such that they take
care of the added enumeration attributes;
defining a Book
::toJSON
function for facilitating the conversion from a
Book
object to a corresponding JSON
record;
defining extensions of the built-in JS object
Array
by adding two class-level functions
(max
and min
) and two instance-level
functions (clone
and isEqualTo
) in
browserShims.js
.
In the user interface ("view") code we have to take care of
adding new table columns in
retrieveAndListAllBooks.html
and suitable
choice widgets in createBook.html
and
upateBook.html
;
creating output for the new attributes in
v/retrieveAndListAllBooks.mjs
;
allowing input for the new attributes in
v/createBook.mjs
and
v/updateBook.mjs
.