3vot-model
Version:
3VOT Model based on SpineJS
61 lines (51 loc) • 2.32 kB
text/coffeescript
ajax_request = require("./ajax_request")
AjaxUtils = require("./ajax_utils")
_3Model = require("3vot-model")
class Singleton
constructor: () ->
= .constructor
reload: (params, options = {}) ->
params.data = .toJSON()
options.url= options.url or AjaxUtils.getURL()
options.error =
ajax_request.queueRequest.get(params, options).end (res) =>
if err then return
else if res.status >= 400 then return
create: (params, options = {}) ->
params.data = .toJSON()
options.url= options.url or AjaxUtils.getCollectionURL()
ajax_request.queueRequest.post(params, options).end (err, res) =>
if err then return
else if res.status >= 400 then return
update: (params, options = {}) ->
params.data = .toJSON()
options.url= options.url or AjaxUtils.getURL()
ajax_request.queueRequest.put(params, options).end (err, res) =>
if err then return
else if res.status >= 400 then return
destroy: (params, options = {}) ->
params.data = .toJSON()
options.url= options.url or AjaxUtils.getURL()
options.error =
ajax_request.queueRequest.del(params, options).end (err, res) =>
if err then return
else if res.status >= 400 then return
# Private
recordResponse: (data, options = {}) =>
ajax_request.disable =>
unless _3Model.isBlank(data) or .destroyed
# ID change, need to do some shifting
if data.id and .id isnt data.id
.changeID(data.id)
# Update with latest data
.refresh(data)
.trigger('ajaxSuccess', data)
options.done?.apply()
failResponse: (error, options = {}) =>
.trigger('ajaxError', error)
options.fail?.apply(, [error] )
module.exports = Singleton