opfplatform
Version:
The celebrated OPF Platform for buidling enterprise Private App Stores
296 lines (239 loc) • 7.31 kB
text/coffeescript
#INCLUDES THE NECCESARY METHODS AND EVENTS TO QUERY AND INSERT OBJECTS TO SALESFORCE
#IT IS ALSO INTEGRATED WITH OTHER PLUGINS LIKE SYNCRONIZABLE, IF IT IS NOT INCLUDED THEN NO PROBLEM
Spine = or require('spine')
$ = Spine.$
Model = Spine.Model
Ajax =
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
beforeSend: (request, settings) ->
ajax: (params, defaults) ->
$.ajax($.extend({}, , defaults, params))
queue: (callback) ->
Ajax.queue(callback)
class Collection extends Base
constructor: () ->
Spine.queries += 1
Spine.trigger "ajaxStart"
find: (id, params , options) ->
record = new
.success()
.error()
all: (filters,params , options) ->
.success()
.error()
query: (filters , params = {}, options = {}) ->
request =
request.success (records) =>
.destroyAll() if .destroyBeforeRefresh
.refresh(records, options)
.trigger "querySuccess"
rest: (params,options = {}) =>
request = .success()
.error()
# Private
recordsResponse: (options = {}) =>
(data, status, xhr) =>
Spine.queries -= 1
Spine.trigger "ajaxEnd"
.trigger('ajaxSuccess', data, status, xhr)
options.onSuccess?(data)
errorResponse: (options = {}) =>
(xhr, statusText, error) =>
Spine.queries -= 1
Spine.trigger "ajaxEnd"
longError = ""
if xhr.status == 503
longError = "Error de Login"
else
errorObj = xhr.responseText
try
parsedErrorObj = JSON.parse errorObj
errorObj = parsedErrorObj[0]
catch err
errorObj =
message: if xhr.responseText and xhr.responseText.length > 0 then xhr.responseText else xhr.statusText
indexS = errorObj.message.indexOf "Exception:"
longError = errorObj.message.substring(indexS)
.trigger('ajaxError', null, longError )
options.onError?( longError)
class Singleton extends Base
constructor: () ->
Spine.queries += 1
Spine.trigger "ajaxStart"
= .constructor
= "#{ @model.overrideClassName or @model.name}__c"
=
fields: .sobjectFormat( , false)
id: .id
objtype:
custom: (method, data, options) ->
=>
request = .success()
.error()
reload: (params, options) ->
=>
request = .success()
.error()
create: (params, options) ->
=>
request = .success()
.error()
update: (params, options) ->
=>
request = .success()
.error()
destroy: (params, options) ->
=>
request = .success()
.error()
# Private
recordResponse: (options = {}) =>
(data, status, xhr) =>
Spine.queries -= 1
Spine.trigger "ajaxEnd"
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.onSuccess?()
errorResponse: (options = {}) =>
(xhr, statusText, error) =>
Spine.queries -= 1
Spine.trigger "ajaxEnd"
errorObj = xhr.responseText
try
parsedErrorObj = JSON.parse errorObj
errorObj = parsedErrorObj[0]
catch err
errorObj =
message: if xhr.responseText and xhr.responseText.length > 0 then xhr.responseText else xhr.statusText
srcError = errorObj.message
indexS = srcError.indexOf "Exception:"
longError = srcError.substring(indexS + 11)
?.trigger('ajaxError', longError)
options.onError?( longError)
# Ajax endpoint
Model.host = '/api/salesforce/sobjects'
Include =
ajax: -> new Singleton(this)
url: (args...) ->
url = Ajax.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.SalesforceAjax =
extended: ->
Extend
Include
# Private
query: ->
.query(arguments...)
rest: ->
.rest(arguments...)
ajaxChange: (record, type, options = {}) ->
return if options.ajax is false
record.ajax()[type](options.ajax, options)
Model.SalesforceAjax.Methods =
extended: ->
Extend
Include
query: ->
.query(arguments...)
rest: ->
.rest(arguments...)
# Globals
Ajax.defaults = Base::defaults
Spine.SalesforceAjax = Ajax
Spine.SalesforceAjaxUtil = new Collection()
Spine.queries = 0
module?.exports = Ajax