tiger
Version:
A full port of Spine.js MVC framework to Titanium Mobile, with enhancements
232 lines (183 loc) • 5.53 kB
text/coffeescript
Tiger = or require './tiger'
Model = Tiger.Model
{Pipeliner} = require '/lib/icedlib'
Ajax =
getURL: (object) ->
object and object.url?() or object.url
enabled: true
disable: (callback) ->
if
= false
try
do callback
catch e
throw e
finally
= true
else
do callback
max: 100
throttle: 0
queue: (request) ->
or= new Pipeliner ,
return .queue unless request
await .waitInQueue defer()
request .defer()
clearQueue: ->
.queue = []
.n_out = 0
class Base
defaults:
contentType: 'application/json'
dataType: 'json'
processData: false
headers: {'X-Requested-With': 'XMLHttpRequest'}
queue: Ajax.queue
ajax: (params, defaults) ->
new Tiger.Ajax
ajaxQueue: (params, defaults) ->
xhr = null
rv = new iced.Rendezvous
settings =
defersuccess = settings.success
defererror = settings.error
settings.success = rv.id('success').defer data, statusText, xhr
settings.error = rv.id('error').defer xhr, statusText, error
request = (next) ->
xhr = new Tiger.Ajax(settings)
await rv.wait defer status
switch status
when 'success' then defersuccess data, statusText, xhr
when 'error' then defererror xhr, statusText, error
next()
request.abort = (statusText) ->
return xhr.abort(statusText) if xhr
index = .indexOf(request)
.splice(index, 1) if index > -1
Ajax.pipeliner.n_out-- if Ajax.pipeliner
# deferred.rejectWith(
# settings.context or settings,
# [xhr, statusText, '']
# )
request
return request unless Ajax.enabled
request
request
ajaxSettings: (params, defaults) ->
Tiger.extend({}, , defaults, params)
class Collection extends Base
constructor: () ->
find: (id, params) ->
record = new
params,
type: 'GET',
url: Ajax.getURL(record)
success:
error:
all: (params) ->
params,
type: 'GET',
url: Ajax.getURL()
success:
error:
fetch: (params = {}, options = {}) ->
if id = params.id
delete params.id
.done (record) =>
.refresh(record, options)
else
.done (records) =>
.refresh(records, options)
# Private
recordsResponse: (data, status, xhr) =>
.trigger('ajaxSuccess', null, status, xhr)
failResponse: (xhr, statusText, error) =>
.trigger('ajaxError', null, xhr, statusText, error)
class Singleton extends Base
constructor: () ->
= .constructor
reload: (params, options) ->
params,
type: 'GET'
url: Ajax.getURL()
success:
error:
create: (params, options) ->
params,
type: 'POST'
data: JSON.stringify()
url: Ajax.getURL()
success:
error:
update: (params, options) ->
params,
type: 'PUT'
data: JSON.stringify()
url: Ajax.getURL()
success:
error:
destroy: (params, options) ->
params,
type: 'DELETE'
url: Ajax.getURL()
success:
error:
# Private
recordResponse: (options = {}) =>
(data, status, xhr) =>
if Spine.isBlank(data)
data = false
else
data = .fromJSON(data)
Ajax.disable =>
if data
# ID change, need to do some shifting
if data.id and .id isnt data.id
.changeID(data.id)
# Update with latest data
.updateAttributes(data.attributes())
.trigger('ajaxSuccess', data, status, xhr)
options.success?.apply() # Deprecated
options.done?.apply()
failResponse: (options = {}) =>
(xhr, statusText, error) =>
.trigger('ajaxError', xhr, statusText, error)
options.error?.apply() # Deprecated
options.fail?.apply()
# Ajax endpoint
Model.host = ''
Include =
ajax: -> new Singleton(this)
url: (args...) ->
url = Ajax.getURL()
url += '/' unless url.charAt(url.length - 1) is '/'
url += Tiger.Ajax.encode()
args.unshift(url)
args.join('/')
Extend =
ajax: -> new Collection(this)
url: (args...) ->
args.unshift(.toLowerCase() + 's')
args.unshift(Model.host)
args.join('/')
Model.Ajax =
extended: ->
Extend
Include
# Private
ajaxFetch: ->
.fetch(arguments...)
ajaxChange: (record, type, options = {}) ->
return if options.ajax is false
record.ajax()[type](options.ajax, options)
Model.Ajax.Methods =
extended: ->
Extend
Include
# Globals
Ajax.defaults = Base::defaults
Tiger.Ajax.ModelAdapter = Ajax
Tiger.Ajax.Q = Base
module?.exports = Ajax