Sunday, June 23, 2013

cordova.js javascript logic

One of the things that makes cordova confuse is it uses the same name for both function parameter and variable.

When cordova.js is called, the outmost function first set the cordova version number and then declare two closure function variables: define and require.
And then it executes another inner function, which implements the define and require function, and modules variable exposed by define.moduleMap.
Up to now, the following variables have been defined inside function scope:
define
require
define.moduleMap
define.remove
When require function is called, it returns the module.exports, which is usually a constructor function to create the module instance. (71:   return modules[id].exports;)

Next, it call define method on cordova and other modules, which will put those modules in modules list. The factory function is not called yet.
define("cordova", function(require, exports, module)...);

define("cordova/argscheck", function(require, exports, module)
define("cordova/channel", function(require, exports, module) 
.....
Then it loads the cordova module with the following line:
window.cordova = require('cordova');
    function build(module) 
    factory:function(require, exports, module). In the factory method, require is the input parameter, container the define function. module is the input parameter represent the current module. exports is the output parameter containing the exported function for the module.
 
The cordova module factory function then load channel by requires it with following code
102: var channel = require('cordova/channel');

For channel:
Note the line 102, var channel = require('cordova/channel');  The var channel contains the exported       javascript object returned to channel.exports. The Channel defined in line
var Channel = function(type, sticky) {
is the prototype construtor for creating an individual channel object, which can handle a particular event
514: var utils = require('cordova/utils'),
After utils module is loaded, channel module register events with the following calls
722: channel.createSticky('onDOMContentLoaded');
725: channel.createSticky('onNativeReady');

107: after channel module is loaded,  cordova module can hookup document event with channel event
186: var cordova = { create the cordova object as exported javascript object
328: expose cordova module by: module.exports = cordova;

6255: (function (context) { call the context method on window object, it will require and load all modules, like contact, and file



 

No comments:

Post a Comment