Adding and optional call to cast Plugin Result

This commit is contained in:
macdonst 2010-11-18 06:37:27 +08:00
parent 46babe7a48
commit c1a87ebaaa
2 changed files with 32 additions and 5 deletions

View File

@ -242,7 +242,7 @@ var Contacts = function() {
* @return array of Contacts matching search criteria
*/
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;
};
/**
* 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.
* @param filter used to match contacts against

View File

@ -421,12 +421,13 @@ PhoneGap.callbackStatus = {
* @param {String} service The name of the service to use
* @param {String} action Action to be run in PhoneGap
* @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 {
var callbackId = service + PhoneGap.callbackId++;
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.
@ -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 (success) {
try {
success(v.message);
if (cast) {
success(cast(v.message));
}
else {
success(v.message);
}
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
@ -504,7 +510,12 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
if (args.status == PhoneGap.callbackStatus.OK) {
try {
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) {