Created
July 20, 2010 16:53
-
-
Save dvv/483215 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CALL: (uri, method, params, headers) -> | |
headers: or {} | |
promise: defer() | |
promise.addErrback (error) -> failure error | |
$.ajax { | |
url: uri | |
contentType: 'application/json' | |
beforeSend: (xhr) -> | |
xhr.setRequestHeader 'client-id', 'dummy' | |
for k, v of headers | |
xhr.setRequestHeader k, v | |
xhr | |
type: method or 'GET' | |
data: if params then JSON.stringify(params) else null | |
dataType: 'json' | |
error: (xhr, textStatus, errorThrown) -> | |
error: errorThrown or xhr.responseText or xhr.statusText or 'Error' | |
console.log xhr | |
promise.reject error | |
success: (data, textStatus, xhr) -> | |
if data and data.error | |
promise.reject data.error | |
else | |
data: or [] | |
#console.log xhr | |
range: xhr.getResponseHeader 'Content-Range' | |
if range | |
range.replace /.*items\s+([0-9]+)-([0-9]+)\/([0-9]+)/, (str, start, end, total) -> | |
data.start: +start | |
data.end: +end | |
data.total: +total | |
#entity: xhr.getResponseHeader 'Content-Type' | |
#if entity | |
# entity.replace /; profile=\.\.\/Class\/(\w+)/, (str, entity) -> | |
# data.entity: entity | |
promise.resolve data | |
} | |
promise | |
... | |
remove: (id) -> | |
CALL entity + '/' + id, 'DELETE', {}, {} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment