Adding a cast for contacts.find()

This commit is contained in:
macdonst 2010-11-19 00:45:36 +08:00
parent c1a87ebaaa
commit 1c5aa6cd00
4 changed files with 24 additions and 21 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], navigator.service.contacts.cast);
PhoneGap.exec(successCB, errorCB, "Contacts", "search", [fields, options]);
};
/**
@ -270,12 +270,13 @@ Contacts.prototype.create = function(properties) {
* @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) {
Contacts.prototype.cast = function(pluginResult) {
var contacts = new Array();
for (var i=0; i<jsonArray.length; i++) {
contacts.push(navigator.service.contacts.create(jsonArray[i]));
for (var i=0; i<pluginResult.message.length; i++) {
contacts.push(navigator.service.contacts.create(pluginResult.message[i]));
}
return contacts;
pluginResult.message = contacts;
return pluginResult;
}
/**

View File

@ -423,11 +423,11 @@ PhoneGap.callbackStatus = {
* @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, cast) {
PhoneGap.exec = function(success, fail, service, action, args) {
try {
var callbackId = service + PhoneGap.callbackId++;
if (success || fail) {
PhoneGap.callbacks[callbackId] = {success:success, fail:fail, cast:cast};
PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
}
// Note: Device returns string, but for some reason emulator returns object - so convert to string.
@ -443,13 +443,8 @@ PhoneGap.exec = function(success, fail, service, action, args, cast) {
// If there is a success callback, then call it now with returned value
if (success) {
try {
if (cast) {
success(cast(v.message));
}
else {
success(v.message);
}
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
}
@ -510,14 +505,9 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
if (args.status == PhoneGap.callbackStatus.OK) {
try {
if (PhoneGap.callbacks[callbackId].success) {
if (PhoneGap.callbacks[callbackId].cast) {
PhoneGap.callbacks[callbackId].success(PhoneGap.callbacks[callbackId].cast(args.message));
}
else {
PhoneGap.callbacks[callbackId].success(args.message);
}
}
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
}

View File

@ -43,7 +43,7 @@ public class ContactManager extends Plugin {
try {
if (action.equals("search")) {
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
return new PluginResult(status, res);
return new PluginResult(status, res, "navigator.service.contacts.cast");
}
else if (action.equals("save")) {
return new PluginResult(status, contactAccessor.save(args.getJSONObject(0)));

View File

@ -14,6 +14,7 @@ public class PluginResult {
private final int status;
private final String message;
private boolean keepCallback = false;
private String cast = null;
public PluginResult(Status status) {
this.status = status.ordinal();
@ -25,6 +26,12 @@ public class PluginResult {
this.message = JSONObject.quote(message);
}
public PluginResult(Status status, JSONArray message, String cast) {
this.status = status.ordinal();
this.message = message.toString();
this.cast = cast;
}
public PluginResult(Status status, JSONArray message) {
this.status = status.ordinal();
this.message = message.toString();
@ -71,7 +78,12 @@ public class PluginResult {
}
public String toSuccessCallbackString(String callbackId) {
return "PhoneGap.callbackSuccess('"+callbackId+"', " + this.getJSONString() + " );";
StringBuffer buf = new StringBuffer("");
if (cast != null) {
buf.append("var temp = "+cast+"("+this.getJSONString() + ");\n");
}
buf.append("PhoneGap.callbackSuccess('"+callbackId+"', temp );");
return buf.toString();
}
public String toErrorCallbackString(String callbackId) {