Novice Learning Cordova-android JS Source (1)

Source: Internet
Author: User

Use PhoneGap fast two years, only recently impulse to see the source code, is too ashamed, can only blame themselves or a JS rookie. So prepare to write something and record it while learning.

I now use the Cordova3.5 version (now 3.6), JS code is probably divided into two pieces: Cordova.js corresponds to the basic framework of Cordova, Android native code is a separate project, as the library is referenced, you can also export jar use; Cordova The plugin module under _plugin.js and plugins, which corresponds to the Java code under the project Org.apache.cordova, can be used to interact with the Android native code and, of course, to define the plugin itself.

First look at Cordova.js, the beginning defines two methods:

  

varrequire, define; (function() {        //map objects for each module        varModules = {},        //Module Request StackRequirestack = [],        //The map object in the Requirestack indexInprogressmodules ={}, SEPARATOR= "."; functionBuild (module) {varFactory =module.factory, Localrequire=function(ID) {varResultantid =ID; //Module ID uses a relative path                if(Id.charat (0) = = = ".") {Resultantid= Module.id.slice (0, Module.id.lastIndexOf (SEPARATOR)) + SEPARATOR + id.slice (2); }                returnrequire (Resultantid);            }; Module.exports= {}; Deletemodule.factory;            Factory (Localrequire, module.exports, module); returnModule.exports; } Require=function(ID) {if(!Modules[id]) {                Throw"MODULE" + ID + "not found"; } Else if(IDinchinprogressmodules) {                //when using the module, there is the case of cyclic require, this time should check your module division.                 varCycle = Requirestack.slice (Inprogressmodules[id]). Join (', ') + ' + 'ID; Throw"Cycle in require graph:" +cycle; }            if(modules[id].factory) {Try{Inprogressmodules[id]=requirestack.length;                    Requirestack.push (ID); returnbuild (Modules[id]); } finally {                    DeleteInprogressmodules[id];                Requirestack.pop (); }            }            //when the module is requested, it is no longer necessary to execute the factory method.             returnModules[id].exports;        }; Define=function(ID, factory) {if(Modules[id]) {Throw"MODULE" + ID + "already defined"; } Modules[id]={id:id, factory:factory};        }; Define.remove=function(ID) {DeleteModules[id];        }; Define.modulemap=modules; })();

Right is require and define, front-end small partners must be very familiar with, and certainly more than I know. But Cordova the two methods here do not implement modular loading, you have to put JS all in.

Define is the definition module,

  

function (Require, exports, module) {});

Require is the use of modules,

  

var cordova = require (' Cordova ');

Look at the code to see the implementation of define, and then see the implementation of require, and finally look at the build method.

Cordova the first part of this, it is not difficult.

Novice Learning Cordova-android JS Source (1)

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.