2. Make a JavaScript Data Model

The starting point for making a JavaScript data model is an association-free information design model where reference properties represent associations. The following model for our example app contains the multi-valued reference property Book::authors, which represents the unidirectional many-to-many association Book-has-Author:

The meaning of the design model and its reference properties publisher and authors can be illustrated by a sample data population for the three model classes:

Table 13.1. Sample data for Publisher

Name Address
Bantam Books New York, USA
Basic Books New York, USA

Table 13.2. Sample data for Book

ISBN Title Year Authors Publisher
0553345842 The Mind's I 1982 1, 2 Bantam Books
1463794762 The Critique of Pure Reason 2011 3
1928565379 The Critique of Practical Reason 2009 3
0465030793 I Am A Strange Loop 2000 2 Basic Books

Table 13.3. Sample data for Author

Author ID Name
1 Daniel Dennett
2 Douglas Hofstadter
3 Immanuel Kant

We now show how to derive a JavaScript data model from the design model in three steps.

  1. Create a check operation for each non-derived property in order to have a central place for implementing property constraints. For any reference property, no matter if single-valued (like Book::publisher) or multi-valued (like Book::authors), the check operation (checkPublisher or checkAuthor) has to check the corresponding referential integrity constraint, which requires that all references reference an existing object, and possibly also a mandatory value constraint, if the property is mandatory.

  2. Create a set operation for each non-derived single-valued property. In the setter, the corresponding check operation is invoked and the property is only set, if the check does not detect any constraint violation.

  3. Create an add, a remove and a set operation for each non-derived multi-valued property. In the case of the Book::authors property, we would create the operations addAuthor, removeAuthor and setAuthors in the Book class rectangle.

This leads to the following JavaScript data model, where we only show the classes Book and Author, while the missing class Publisher is the same as in Figure 12.1:

Notice that, for simplicity, we do not include the code for all validation checks shown in the data model in the code of the example app.