日期:2014-05-16  浏览次数:20372 次

PhoneGap 1.5版本 cordova.js 简析 3
在基本升级了phonegap1.5后,发现原有的phonegap插件基本还是能够工作的,而因为项目原因我需要重写phonegap的定位能力,却发现无法找到类似phonegap 1.4的定义代码
	PhoneGap.addConstructor(function() {
				navigator._geo = new Geolocation();

				// No native geolocation object for Android 1.x, so use PhoneGap
				// geolocation
				if (typeof navigator.geolocation === 'undefined') {
					navigator.geolocation = navigator._geo;
					Geolocation.usingPhoneGap = true;
				}

				//Geolocation.usePhoneGap();
			});




进过分析发现phonegap通过一个'cordova/common'模块保证我们继续可以使用navigator.geolocation来访问函数.在phonegap1.5 js代码的最后实现了一个self.boot的方法,当onNativeReady时触发,下面就是其中的主要功能 -- 扩展window对象
				// Drop the common globals into the window object, but be nice
				// and don't overwrite anything.
				builder.build(base.objects).intoButDontClobber(window);

				// Drop the platform-specific globals into the window object and
				// do it like a honey badger does it.
				builder.build(platform.objects).intoAndClobberTheFOutOf(window);

				// Call the platform-specific initialization
				platform.initialize();



整个过程分成了三部分, 构建共用对象,构建平台(IOS, android等)对象,初始化平台参数. 而前两者都是通过'cordova/builder'组建实现.通过分析该组建可以发现其中最重要的函数为include,代码如下
function include(parent, objects, clobber) {
	each(objects, function(obj, key) {
				try {
					var result = obj.path ? require(obj.path) : {};

					if (clobber) {
						// Clobber if it doesn't exist or if an
						// override is specified.
						if (typeof parent[key] === 'undefined'
								|| typeof obj.path !== 'undefined') {
							parent[key] = result;
						}
						result = parent[key];
					} else {
						// Don't clobber if something already exists
						// there
						if (typeof parent[key] == 'undefined') {
							parent[key] = result;
						} else {
							// Set result to what already exists, so
							// we can build children into it if they
							// exist.
							result = parent[key];
						}
					}

					if (obj.children) {
						include(result, obj.children, clobber);
					}
				} catch (e) {
					alert('Exception building cordova JS globals: '
							+ e + ' for key "' + key + '"');
				}
			});
}


通过include函数,程序会把在common中定义的object下的所有对象通过path初始化后以属性的方式添加进windows里面,但是如果window对象中存在了该属性则不会被覆盖.同时一个功能如果需要存在子结点,则必须放在obj.children里(同path同级)否则无效.


接着程序会将platform的object下对象,以覆盖的方式添加进window中,主要包括以下几个属性
window.cordova.JSCallback : "cordova/plugin/android/callback"
window.cordova.JSCallbackPolling : "cordova/plugin/android/polling"
window.navigator.app : "cordova/plugin/android/app"
window.device : "cordova/plugin/android/device"
window.File : "cordova/plugin/File"
window.FileReader : "cordova/plugin/FileReader"
window.FileError : "cordova/plugin/FileError"


而如我开始的需求,则需要将 navigator.geolocation 从common 移到 platform中保证在android平台强制使用扩展的定位能力,而非浏览器自带的.
1 楼 jkljkl 2012-06-04  
麻烦问一下,“将 navigator.geolocation 从common 移到 platform中”,具体是怎么做的?
我最近也在做phonegap,需要用到定位,正在android的这个问题困扰