Popush source code Learning report

Source: Internet
Author: User

@ Xiao Jiannan 2011013223

Collation

Popush adopts the NodeJS architecture, Socket. IO is used for network communication, and the MVC architecture is implemented based on Backbone. js. The data interaction part with Mongodb is encapsulated into a Model in the/models folder. The part that defines the surface interaction mode is encapsulated into a View in the/static/js/views folder, in addition, the code in/static/js/room implements code editing, real-time chat (incomplete), and other functions,/static/js/router. js defines the server route, and the app under the root directory. js completes server startup and event listening processing.

C/S architecture interaction mode

The network communication of Popush is based on Socket. IO. When app. js is started using node, the server starts to listen to socket connections. After the connection, the server automatically processesVersion, register, reloginRequest, as defined in the following method

Socket. on ('request _ name', function (data) {/* do something */});

When the client sends a request, follow these steps:

App. socket. emit ('request _ name', {para1: value1 ,...});
View module

In/static/index.html (561-572), a series of *-view. js files are referenced.

<Script type = "text/javascript" src = "js/views/login-view.js"> </script> <script type = "text/javascript" src = "js/views/register-view.js "> </script> ......

In these files, there is a similar code

App. views ['register '] = new app. RegisterView (); OR app. views ['account'] = new app. AccountView ();

This code initializes all pages and stores them in global variables.AppOfViewsAttribute to facilitate timely calling.App. * ViewThe definition consists of the following parts:Events, initialize ,..., WhereEventsDefines the response events on the page,InitializeDefines the page initialization logic, and there are other methods, mainly used to define the event processing logic.

Model module

Direct interaction with Mongodb data is defined in/models/* DAO. in the js file, there are two files in total: docDAO. js and userDAO. js, which supports adding, deleting, querying, and updating user information and document content. All database-related operations are performed by variablesDbDone,DbPassRequireDatabase configuration to obtain a connection-like variable.Insert, findOne, find, updateTo interact with the database. Because Mongodb is an object-type database, you only need to pass the query conditions in JSON format as parameters, and you do not need to write complex SQL statements.

Business implementation logic example analysis

Take file sharing as an example:

  1. Client request file-view.js (13), inEventsDefined inA. file-go-shareThe click event processing function isShare,

    App. views. shares. using model = this. model; // Obtain the app that references the model. collections ['shares']. set (this. model. get ('members '); // get the member list of this file $ (' # share '). modal ('Show '); // display the share dialog box

  2. It is worth mentioning that SharerlistView correspondsApp. collections ['share']In the SharerlistViewElAttribute defines the location of elements bound in html.# ShareIs called by the function share in the file-view.js# ShareYou can understand that the *-view. js file dynamically generates corresponding tags andIdOther functions can be accessed through jQuery selector.

  3. In the share dialog box, after selecting a user, click the share button to trigger the share function, the binding is defined in sharerlist-view.js (9), then get the parameter, after verification, callApp. socket. emit ('share', {/* params */})Send the request to the server.

  4. Server listenerShareRequest, defined in app. js (346). If the session fails, 'authorized' is returned. OtherwiseDocDAOOfAddMemberThe function adds the record to the database and returns'Share'Response. If no exception occurs, broadcast the information to all other users who are accessing the file. The client receives'Share'After the response, the detach function is called to execute relevant processing.

Popush source code Learning report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.