opfplatform
Version:
The celebrated OPF Platform for buidling enterprise Private App Stores
237 lines (193 loc) • 5.61 kB
text/coffeescript
Spine = or require('spine')
$ = Spine.$
Model = Spine.Model
$.ajaxSetup
headers:
"X-Parse-Application-Id" : "",
"X-Parse-REST-API-Key": ""
ParseAjax =
getURL: (object) ->
object and object.url?() or object.url
enabled: true
pending: false
requests: []
disable: (callback) ->
if
= false
try
do callback
catch e
throw e
finally
= true
else
do callback
requestNext: ->
next = .shift()
if next
else
= false
request: (callback) ->
(do callback).complete(=> do )
queue: (callback) ->
return unless
if
.push(callback)
else
= true
callback
class Base
defaults:
contentType: 'application/json'
dataType: 'json'
processData: false
ajax: (params, defaults) ->
$.ajax($.extend({}, , defaults, params))
queue: (callback) ->
ParseAjax.queue(callback)
class Collection extends Base
constructor: () ->
custom: ( method , params , options) ->
url = params.url || ParseAjax.getURL()
params.data = JSON.stringify(params.data) if params.data and typeof params.data is "object"
.success( )
.error( )
find: (id, params) ->
record = new
.success()
.error()
all: (params) ->
.success()
.error()
fetch: (params = {}, options = {}) ->
if id = params.id
delete params.id
.success (record) =>
.refresh(record, options)
else
.success (records) =>
.refresh(records, options)
# Private
customResponse: (options = {}) =>
(data, status, xhr) =>
= Spine if ! # to allow external access
.trigger('customParseAjaxSuccess', data, status, xhr)
options.success?.apply( , [data])
recordsResponse: (data, status, xhr) =>
.trigger('ajaxSuccess', null, status, xhr)
errorResponse: (xhr, statusText, error) =>
.trigger('ajaxError', null, xhr, statusText, error)
customErrorResponse: (options = {}) =>
(xhr, statusText, error) =>
= Spine if ! # to allow external access
.trigger('ajaxError', xhr, statusText, error)
options.error?.apply( , [xhr, statusText, error] )
class Singleton extends Base
constructor: () ->
= .constructor
reload: (params, options) ->
=>
.success()
.error()
create: (params, options) ->
=>
.success()
.error()
update: (params, options) ->
data = .forSave?()
=>
.success()
.error()
destroy: (params, options) ->
=>
.success()
.error()
# Private
recordResponse: (options = {}) =>
(data, status, xhr) =>
if Spine.isBlank(data)
data = false
else
data = .fromJSON(data)
ParseAjax.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()
errorResponse: (options = {}) =>
(xhr, statusText, error) =>
.trigger('ajaxError', xhr, statusText, error)
options.error?.apply(, [xhr, statusText, error] )
# ParseAjax endpoint
Model.host = 'https://api.parse.com/1'
Include =
ajax: -> new Singleton(this)
url: (args...) ->
url = ParseAjax.getURL()
url += '/' unless url.charAt(url.length - 1) is '/'
url += encodeURIComponent()
args.unshift(url)
args.join('/')
Extend =
ajax: -> new Collection(this)
url: (args...) ->
args.unshift(.toLowerCase() + 's')
args.unshift(Model.host)
args.join('/')
Model.ParseAjax =
extended: ->
Extend
Include
# Private
ajaxFetch: ->
.fetch(arguments...)
ajaxChange: (record, type, options = {}) ->
return if options.ajax is false
record.ajax()[type](options.ajax, options)
Model.ParseAjax.Methods =
extended: ->
Extend
Include
# Globals
ParseAjax.defaults = Base::defaults
Spine.ParseAjax = ParseAjax
Spine.ParseAjaxUtil = new Collection()
module?.exports = ParseAjax