Refactor out the Java casting code

When we return JSON to the Java side it does not have the proper methods such as Contact.save() so we need to cast the JSON to the correct JS object. This used to be done from the Java layer calling the right method to cast the JSON. In this new approach the JavaScript layer will no what needs to be cast and call it's own internal function to do the cast.
This commit is contained in:
macdonst
2012-02-08 14:12:06 -05:00
parent 77547f8826
commit 9aa1cd756a
12 changed files with 260 additions and 187 deletions
+8 -19
View File
@@ -259,7 +259,14 @@ Contacts.prototype.find = function(fields, successCB, errorCB, options) {
errorCB({"code": ContactError.INVALID_ARGUMENT_ERROR});
}
} else {
Cordova.exec(successCB, errorCB, "Contacts", "search", [fields, options]);
var win = function(result) {
var cs = [];
for (var i = 0, l = result.length; i < l; i++) {
cs.push(navigator.contacts.create(result[i]));
}
successCB(cs);
};
Cordova.exec(win, errorCB, "Contacts", "search", [fields, options]);
}
};
@@ -281,24 +288,6 @@ 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.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(pluginResult) {
var contacts = [];
var i;
for (i=0; i<pluginResult.message.length; i++) {
contacts.push(navigator.contacts.create(pluginResult.message[i]));
}
pluginResult.message = contacts;
return pluginResult;
};
/**
* ContactFindOptions.
* @constructor