Folder Structure and Library Files
Let’s take a look at some library files and how to set up the folder structure.
We'll cover the following
The MVC folder structure of our validation application extends the structure of the minimal application by adding a lib
folder containing the generic code libraries browserShims.js
, errorTypes.js
, and util.js
.
As a result, we get the following folder structure:
publicLibrary
css
main.css
normalize.min.css
lib
browserShims.js
errorTypes.js
util.js
src
c
m
v
index.html
We discuss the contents of the added files in the following sub-sections.
General utility functions and library files fixes
We add three library files to the lib
folder:
-
browserShims.js
: This contains a definition of the string trim function for older browsers that don’t support this function (which was only added to JavaScript in ES5, defined in 2009). More browser shims for other recently defined functions, such asquerySelector
andclassList
, could also be added tobrowserShims.js
. -
util.js
: This contains the definitions of a few utility functions such asisNonEmptyString(x)
to test ifx
is a non-empty string. -
errorTypes.js
: This defines classes for error (or exception) types that correspond to the basic types of [property constraints], listed as follows: (https://www.educative.io/collection/page/10370001/5397377793392640/5644866681307136):StringLengthConstraintViolation
MandatoryValueConstraintViolation
RangeConstraintViolation
IntervalConstraintViolation
PatternConstraintViolation
UniquenessConstraintViolation
In addition, a class NoConstraintViolation
is defined to allow us to return a validation result object in the case of no constraint violation.
The start page
The start page index.html
takes care of loading CSS page styling files with the help of the following two link elements:
<link rel="stylesheet" href="css/normalize.min.css">
<link rel="stylesheet" href="css/main.css">
Then it loads the following JavaScript files:
-
browserShims.js
andutil.js
from thelib
folder. -
errorTypes.js
from thelib
folder, a file that defines exception classes. -
initialize.js
from thesrc/c
folder, a file that defines the app’s MVC namespaces. -
Book.js
from thesrc/m
folder, a model class file that provides data management and other functions.
You can click the “Run” button below to launch the application. The view and model code does not have the constraint validation in place yet.
Note: We’ve added an
Edition
column but its entries do not show up on the retrieve view. This is because we haven’t updated the view code yet. You can check out thesrc/v/retrieveAndListAll.js
file and notice that we’re only retrievingisbn
,title
, andyear
for now.
Get hands-on with 1400+ tech skills courses.