concept fake module in category javascript
appears as: Fake modules, Fake module, Fake modules

This is an excerpt from Manning's book Single Page Web Applications: JavaScript end-to-end.
Listing 5.12. Start building the Model—spa/js/spa.model.js
/* * spa.model.js * Model module */ /*jslint browser : true, continue : true, devel : true, indent : 2, maxerr : 50, newcap : true, nomen : true, plusplus : true, regexp : true, sloppy : true, vars : false, white : true */ /*global TAFFY, $, spa */ spa.model = (function () { 'use strict'; var configMap = { anon_id : 'a0' }, #1 stateMap = { anon_user : null, #2 people_cid_map : {}, #3 people_db : TAFFY() #4 }, isFakeData = true, #5 personProto, makePerson, people, initModule; personProto = { #6 get_is_user : function () { return this.cid === stateMap.user.cid; }, get_is_anon : function () { return this.cid === stateMap.anon_user.cid; } }; makePerson = function ( person_map ) { #7 var person, cid = person_map.cid, css_map = person_map.css_map, id = person_map.id, name = person_map.name; if ( cid === undefined || ! name ) { throw 'client id and name required'; } person = Object.create( personProto ); #8 person.cid = cid; person.name = name; person.css_map = css_map; if ( id ) { person.id = id; } stateMap.people_cid_map[ cid ] = person; stateMap.people_db.insert( person ); return person; }; people = { #9 get_db : function () { return stateMap.people_db; }, #10 get_cid_map : function () { return stateMap.people_cid_map; } #11 }; initModule = function () { var i, people_list, person_map; // initialize anonymous person #12 stateMap.anon_user = makePerson({ cid : configMap.anon_id, id : configMap.anon_id, name : 'anonymous' }); stateMap.user = stateMap.anon_user; if ( isFakeData ) { #13 people_list = spa.fake.getPeopleList(); for ( i = 0; i < people_list.length; i++ ) { person_map = people_list[ i ]; makePerson({ cid : person_map._id, css_map : person_map.css_map, id : person_map._id, name : person_map.name }); } } }; return { initModule : initModule, people : people }; }()); #1: Reserve a special ID for the “anonymous” person. #2: Reserve the anon_user key in our state map to store the anonymous person object. #3: Reserve the people_cid_map key in our state map to store a map of person objects keyed by client ID. #4: Reserve the people_db key in our state map to store a TaffyDB collection of person objects. Initialize it as an empty collection. #5: Set isFakeData to true. This flag tells the Model to use the example data, objects, and methods from the Fake module instead of actual data from the Data module. #6: Create a prototype for person objects. Use of a prototype usually reduces memory requirements and improves the performance of objects. #7: Add a makePerson method that creates a person object and stores it in a TaffyDB collection. Ensure it also updates the index in the people_cid_map. #8: Use Object.create ( <prototype> ) to create our object from a prototype and then add instance-specific properties. #9: Define the people object. #10: Add the get_db method to return the TaffyDB collection of person objects. #11: Add the get_cid_map method to return a map of person objects keyed by client ID. #12: Make the anonymous person object in initModule to ensure it has the same methods and attributes of other person objects regardless of future changes. This is an example of “design for quality.” #13: Get the list of online people from the Fake module and add them to the people_db TaffyDB collection.