...

/

Model Code: Serialization and Data Management

Model Code: Serialization and Data Management

Let’s learn how to perform serialization and data management operations on the model.

Add a serialization function

It’s helpful to have an object serialization function tailored to the structure of an object (as defined by its class) such that the result of serializing an object is a human-readable string representation of the object showing all its relevant information items. By convention, these functions are called toString().

In the case of the Book class, we use the following code:

Press + to interact
Book.prototype.toString = function () {
return "Book{ ISBN:" + this.isbn + ", title:" + this.title +
", year:" + this.year + (this.edition || "") + "}";
};

Data management operations

We’ve defined the model class in the form of a constructor function with property definitions, checks, and setters—as well as a toString() serialization function. We also need to define the following data management operations as class-level methods of the model class:

  1. Book.convertRec2Obj and Book.retrieveAll for loading all managed Book instances from the persistent data store.
  2. Book.saveAll for saving all managed Book instances to the persistent data store.
  3. Book.add for
...