mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 03:53:09 +08:00
Adding and optional call to cast Plugin Result
This commit is contained in:
parent
46babe7a48
commit
c1a87ebaaa
@ -242,7 +242,7 @@ var Contacts = function() {
|
|||||||
* @return array of Contacts matching search criteria
|
* @return array of Contacts matching search criteria
|
||||||
*/
|
*/
|
||||||
Contacts.prototype.find = function(fields, successCB, errorCB, options) {
|
Contacts.prototype.find = function(fields, successCB, errorCB, options) {
|
||||||
PhoneGap.exec(successCB, errorCB, "Contacts", "search", [fields, options]);
|
PhoneGap.exec(successCB, errorCB, "Contacts", "search", [fields, options], navigator.service.contacts.cast);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -262,6 +262,22 @@ Contacts.prototype.create = function(properties) {
|
|||||||
return contact;
|
return contact;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function returns and array of contacts. It is required as we need to convert raw
|
||||||
|
* JSON objects into concrete Contact objects. Currently this method is called after
|
||||||
|
* navigator.service.contacts.find but before the find methods success call back.
|
||||||
|
*
|
||||||
|
* @param jsonArray an array of JSON Objects that need to be converted to Contact objects.
|
||||||
|
* @returns an array of Contact objects
|
||||||
|
*/
|
||||||
|
Contacts.prototype.cast = function(jsonArray) {
|
||||||
|
var contacts = new Array();
|
||||||
|
for (var i=0; i<jsonArray.length; i++) {
|
||||||
|
contacts.push(navigator.service.contacts.create(jsonArray[i]));
|
||||||
|
}
|
||||||
|
return contacts;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ContactFindOptions.
|
* ContactFindOptions.
|
||||||
* @param filter used to match contacts against
|
* @param filter used to match contacts against
|
||||||
|
@ -421,12 +421,13 @@ PhoneGap.callbackStatus = {
|
|||||||
* @param {String} service The name of the service to use
|
* @param {String} service The name of the service to use
|
||||||
* @param {String} action Action to be run in PhoneGap
|
* @param {String} action Action to be run in PhoneGap
|
||||||
* @param {String[]} [args] Zero or more arguments to pass to the method
|
* @param {String[]} [args] Zero or more arguments to pass to the method
|
||||||
|
* @param {String} jsClass The class to cast the return as
|
||||||
*/
|
*/
|
||||||
PhoneGap.exec = function(success, fail, service, action, args) {
|
PhoneGap.exec = function(success, fail, service, action, args, cast) {
|
||||||
try {
|
try {
|
||||||
var callbackId = service + PhoneGap.callbackId++;
|
var callbackId = service + PhoneGap.callbackId++;
|
||||||
if (success || fail) {
|
if (success || fail) {
|
||||||
PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
|
PhoneGap.callbacks[callbackId] = {success:success, fail:fail, cast:cast};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Device returns string, but for some reason emulator returns object - so convert to string.
|
// Note: Device returns string, but for some reason emulator returns object - so convert to string.
|
||||||
@ -442,7 +443,12 @@ PhoneGap.exec = function(success, fail, service, action, args) {
|
|||||||
// If there is a success callback, then call it now with returned value
|
// If there is a success callback, then call it now with returned value
|
||||||
if (success) {
|
if (success) {
|
||||||
try {
|
try {
|
||||||
success(v.message);
|
if (cast) {
|
||||||
|
success(cast(v.message));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
success(v.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("Error in success callback: "+callbackId+" = "+e);
|
console.log("Error in success callback: "+callbackId+" = "+e);
|
||||||
@ -504,7 +510,12 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
|
|||||||
if (args.status == PhoneGap.callbackStatus.OK) {
|
if (args.status == PhoneGap.callbackStatus.OK) {
|
||||||
try {
|
try {
|
||||||
if (PhoneGap.callbacks[callbackId].success) {
|
if (PhoneGap.callbacks[callbackId].success) {
|
||||||
PhoneGap.callbacks[callbackId].success(args.message);
|
if (PhoneGap.callbacks[callbackId].cast) {
|
||||||
|
PhoneGap.callbacks[callbackId].success(PhoneGap.callbacks[callbackId].cast(args.message));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PhoneGap.callbacks[callbackId].success(args.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user