Api.coffeesrc/ | |
---|---|
Provides an interface to the resource api on the server via AJAX | `define(['Q', './Events'], function(Q, Events){`
Api = new Class
Implements: [Events] |
Model api | |
To perform api calls, the Model class will
call this method and pass simply the operation being performed and any
paramteres as needed. The Collection class
also uses the Default Operations
Can also be used for generic actions on models
Handling Operation CompletionTo signal when the operation is complete you have a choice of using events or using the returned promise.
| api: (operation, data={}, options={}) ->
deferred = Q.defer()
method = @_getRequestMethod operation
sendDataAsJson = @_sendDataAsJson operation
fail = (reason=null) =>
deferred.reject reason
fireEvent "failure", [reason]
fireEvent = (event, args) =>
eventName = "#{options.eventName or operation}#{event.capitalize()}"
@fireEvent eventName, args unless options.silent
if sendDataAsJson
requestData = JSON.encode data
urlEncoded = false
headers = {
'Content-type': 'application/json'
}
else
requestData = data
urlEncoded = true
headers = {}
new Request.JSON
url: @_getUrl operation
method: method
headers: headers
data: requestData
urlEncoded: urlEncoded
onRequest: => fireEvent "start"
onComplete: => fireEvent "complete"
onFailure: (xhr) => fail()
onSuccess: (response) =>
if @isSuccess response
data = @parseResponse response
deferred.resolve data
fireEvent "success", [data]
else
reason = @parseFailResponse response
fail reason
.send()
return deferred.promise |
Response parsing | |
If the request itself has succeeded then this function is called with the response to determine if the operation has indeed succeeded. | isSuccess: (response) ->
response.success is true |
If the operation has succeeded then this is called to extract the data returned in the response | parseResponse: (response) ->
response.data |
If the operation has failed then this is called to extract the reason | parseFailResponse: (response) ->
response.error |
Determining the correct URL | |
This determines the correct URL for the given operation. The base URL
is expected either be For the various operations the url will be
For unknown actions the url will default to
| _getUrl: (operation) ->
url = @url
if not url? and @collection?
url = @collection.url
if not url?
throw new Error "No url can be found"
def = _methodDefinitions[operation]
if def
urlScheme = _urlSchemes[def.scheme]
else
urlScheme = _urlSchemes.method
operationUrl = urlScheme.substitute
baseUrl: url
id: @id
method: operation
return operationUrl
_getRequestMethod: (operation) ->
def = _methodDefinitions[operation]
if def?
def.method
else
'post'
_sendDataAsJson: (operation) ->
def = _methodDefinitions[operation]
if def?
def.json or false
else
false
_urlSchemes =
file: "{baseUrl}"
directory: "{baseUrl}/"
id: "{baseUrl}/{id}"
method: "{baseUrl}/{id}/{method}" |
Defines what urlschemes and request methods to use for each method. | _methodDefinitions =
create:
method: 'post'
scheme: 'file'
json: true
read:
method: 'get'
scheme: 'id'
update:
method: 'put'
scheme: 'id'
json: true
delete:
method: 'delete'
scheme: 'id'
list:
method: 'get'
scheme: 'directory'
return Api
`})`
|