diff --git a/RemoteTK.component b/RemoteTK.component index 071114a..6033b69 100644 --- a/RemoteTK.component +++ b/RemoteTK.component @@ -32,21 +32,32 @@ if (remotetk === undefined) { } if (remotetk.Client === undefined) { - function handleResult(result, callback, error, nullok) { + function handleResult(result, callback, error, nullok, deferred) { if (result) { result = JSON.parse(result); if ( Array.isArray(result) && result[0].message && result[0].errorCode ) { if ( typeof error === 'function' ) { error(result); } + deferred.reject(result); } else { - callback(result); + if ( typeof callback === 'function' ) { + callback(result); + } + deferred.resolve(result); } } else if (typeof nullok !== 'undefined' && nullok) { - callback(); + if ( typeof callback === 'function' ) { + callback(); + } + deferred.resolve(); } else { - error([{ message : "Null return from action method","errorCode":"NULL_RETURN"}]); - } + var errorResult = [{ message : "Null return from action method","errorCode":"NULL_RETURN"}]; + if ( typeof error === 'function' ) { + error(errorResult); + } + deferred.reject(errorResult); + } } /** @@ -72,11 +83,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.describe = function(objtype, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.describe}', objtype, function(result){ - handleResult(result, callback, error); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.describe}', objtype, function(result, err) { + if (err.status) { + handleResult(result, callback, error, false, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } /* @@ -89,11 +109,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.create = function(objtype, fields, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.create}', objtype, JSON.stringify(fields), function(result){ - handleResult(result, callback, error); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.create}', objtype, JSON.stringify(fields), function(result, err) { + if (err.status) { + handleResult(result, callback, error, false, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } /* @@ -106,11 +135,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.retrieve = function(objtype, id, fieldlist, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.retrieve}', objtype, id, fieldlist, function(result){ - handleResult(result, callback, error); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.retrieve}', objtype, id, fieldlist, function(result, err) { + if (err.status) { + handleResult(result, callback, error, false, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } /* @@ -126,11 +164,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.upsert = function(objtype, externalIdField, externalId, fields, callback, error) { - Visualforce.remoting.Manager.invokeAction('$RemoteAction.RemoteTKController.upser', objtype, externalIdField, externalId, JSON.stringify(fields), function(result){ - handleResult(result, callback, error, true); - }, { - escape: false + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('$RemoteAction.RemoteTKController.upser', objtype, externalIdField, externalId, JSON.stringify(fields), function(result, err) { + if (err.status) { + handleResult(result, callback, error, true, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { + escape: false }); + return deferred.promise(); } /* @@ -144,11 +191,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.update = function(objtype, id, fields, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.updat}', objtype, id, JSON.stringify(fields), function(result){ - handleResult(result, callback, error, true); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.updat}', objtype, id, JSON.stringify(fields), function(result, err) { + if (err.status) { + handleResult(result, callback, error, true, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } /* @@ -160,11 +216,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.del = function(objtype, id, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.del}', objtype, id, function(result){ - handleResult(result, callback, error, true); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.del}', objtype, id, function(result, err) { + if (err.status) { + handleResult(result, callback, error, true, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } /* @@ -175,12 +240,21 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.query = function(soql, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.query}', soql, function(result){ - handleResult(result, callback, error); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.query}', soql, function(result, err) { + if (err.status) { + handleResult(result, callback, error, false, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); - } + return deferred.promise(); + } /* * Executes the specified SOSL search. @@ -190,11 +264,20 @@ if (remotetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ remotetk.Client.prototype.search = function(sosl, callback, error) { - Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.search}', sosl, function(result){ - handleResult(result, callback, error); - }, { + var deferred = $.Deferred(); + Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.search}', sosl, function(result, err) { + if (err.status) { + handleResult(result, callback, error, false, deferred); + } else { + if (typeof error === 'function') { + error(err); + } + deferred.reject(err); + } + }, { escape: false }); + return deferred.promise(); } }