concept Data module in category javascript

appears as: Data module, The Data module
Single Page Web Applications: JavaScript end-to-end

This is an excerpt from Manning's book Single Page Web Applications: JavaScript end-to-end.

In this chapter we design and build the people object portion of the Model. The Model provides the business logic and data to the Shell and feature modules. The Model is independent of the user interface (UI) and isolates it from logic and data management. The Model is itself isolated from the web server through the use of a Data module.

In this chapter we design and build the chat object portion of the Model. We then complete the Chat slider UI by having it use and respond to the chat object API. We also add an Avatar feature module which also uses the chat object API to display on-screen representations of online people. We discuss how we accomplish data binding using jQuery. Finally, we complete the client portion of the SPA with the addition of the Data module.

6.7. Create the Data module

In this section we create the Data module as shown in figure 6.11.

Next we update the Data module as shown in listing 6.21. This module manages all the connections to the server in our architecture, and all data communicated between the client and server flows through this module. All that this module does may not be clear at present, but don’t worry—we’ll cover Socket.IO in further detail in the next chapter. Changes are shown in bold:

Listing 6.21. Update the Data module—spa/js/spa.data.js
... /*global $, io, spa */ spa.data = (function () { 'use strict'; var stateMap = { sio : null }, makeSio, getSio, initModule; makeSio = function (){ var socket = io.connect( '/chat' ); #1 return { #2 emit : function ( event_name, data ) { #3 socket.emit( event_name, data ); }, on : function ( event_name, callback ) { #4 socket.on( event_name, function (){ callback( arguments ); }); } }; }; getSio = function (){ #5 if ( ! stateMap.sio ) { stateMap.sio = makeSio(); } return stateMap.sio; }; initModule = function (){}; #6 return { #7 getSio : getSio, initModule : initModule }; }()); #1 Create the socket connection using the /chat namespace. #2 Write the code to return our methods for an sio object #3 Ensure the emit method sends data associated with a given event name to the server. #4 Ensure the on method registers a callback for a given event name. Any event data received from the server will be passed back to the callback #5 Create a getSio method, which tries to always return a valid sio object. #6 Create an initModule method. This doesn’t do anything yet, but we always want to ensure it’s available and that our root namespace module (spa/js/spa.js) invokes it before the initialization of the Model or the Shell #7 Neatly export all public data methods !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":15}],[{\"line\":6,\"ch\":5},{\"line\":6,\"ch\":8}],[{\"line\":26,\"ch\":13},{\"line\":26,\"ch\":16}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":3}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":3}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":26}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":28}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":22}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":32}],[{\"line\":5,\"ch\":24},{\"line\":5,\"ch\":26}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":35}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":22}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":5,\"ch\":24},{\"line\":5,\"ch\":25}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":1}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":1}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":1}],[{\"line\":21,\"ch\":50},{\"line\":21,\"ch\":51}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":1}],[{\"line\":24,\"ch\":25},{\"line\":24,\"ch\":26}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":1}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":1}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":24,\"ch\":25},{\"line\":24,\"ch\":27}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":24,\"ch\":25},{\"line\":24,\"ch\":27}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":51}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":20}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":24,\"ch\":25},{\"line\":24,\"ch\":27}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":26,\"ch\":0},{\"line\":26,\"ch\":16}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":23}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":24,\"ch\":25},{\"line\":24,\"ch\":27}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":30,\"ch\":4},{\"line\":30,\"ch\":59}],[{\"line\":9,\"ch\":16},{\"line\":9,\"ch\":18}],[{\"line\":31,\"ch\":0},{\"line\":31,\"ch\":2}],[{\"line\":31,\"ch\":4},{\"line\":31,\"ch\":58}],[{\"line\":10,\"ch\":41},{\"line\":10,\"ch\":43}],[{\"line\":32,\"ch\":0},{\"line\":32,\"ch\":2}],[{\"line\":32,\"ch\":4},{\"line\":32,\"ch\":72}],[{\"line\":32,\"ch\":74},{\"line\":32,\"ch\":88}],[{\"line\":13,\"ch\":42},{\"line\":13,\"ch\":44}],[{\"line\":33,\"ch\":0},{\"line\":33,\"ch\":2}],[{\"line\":33,\"ch\":4},{\"line\":33,\"ch\":24}],[{\"line\":33,\"ch\":26},{\"line\":33,\"ch\":94}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":51}],[{\"line\":20,\"ch\":27},{\"line\":20,\"ch\":29}],[{\"line\":35,\"ch\":0},{\"line\":35,\"ch\":2}],[{\"line\":35,\"ch\":4},{\"line\":35,\"ch\":76}],[{\"line\":24,\"ch\":32},{\"line\":24,\"ch\":34}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":36,\"ch\":4},{\"line\":36,\"ch\":220}],[{\"line\":25,\"ch\":16},{\"line\":25,\"ch\":18}],[{\"line\":37,\"ch\":0},{\"line\":37,\"ch\":2}],[{\"line\":37,\"ch\":4},{\"line\":37,\"ch\":41}]]"} !@%STYLE%@!