PhoneGap 1.5版本 cordova.js 简析 3

文章详细介绍了如何在PhoneGap 1.5中自定义定位插件,包括对PhoneGap 1.5架构的理解,以及如何通过调整common和platform对象来实现特定需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在基本升级了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平台强制使用扩展的定位能力,而非浏览器自带的.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值