Update to spinejs 1.6.1.

This commit is contained in:
Martin Edenhofer 2015-12-21 22:06:07 +01:00
parent b990fa6daf
commit 994c363484
2 changed files with 48 additions and 33 deletions

View file

@ -95,10 +95,15 @@ class Base
# 2 reasons not to stringify: if already a string, or if intend to have ajax processData # 2 reasons not to stringify: if already a string, or if intend to have ajax processData
if typeof settings.data isnt 'string' and settings.processData isnt true if typeof settings.data isnt 'string' and settings.processData isnt true
settings.data = JSON.stringify(settings.data) settings.data = JSON.stringify(settings.data)
# enable promise callbacks to access the request's settings object
resolve = ->
deferred.resolve.apply this, [arguments..., settings]
reject = ->
deferred.reject.apply this, [arguments..., settings]
jqXHR = $.ajax(settings) jqXHR = $.ajax(settings)
.done(deferred.resolve) jqXHR.done(resolve)
.fail(deferred.reject) jqXHR.fail(reject)
.then(next, next) jqXHR.then(next, next)
if parallel if parallel
Queue.dequeue() Queue.dequeue()
@ -129,8 +134,8 @@ class Collection extends Base
url: options.url or Ajax.getURL(record) url: options.url or Ajax.getURL(record)
parallel: options.parallel parallel: options.parallel
} }
).done(@recordsResponse) ).done(@recordsResponse(options))
.fail(@failResponse) .fail(@failResponse(options))
all: (params, options = {}) -> all: (params, options = {}) ->
@ajaxQueue( @ajaxQueue(
@ -139,8 +144,8 @@ class Collection extends Base
url: options.url or Ajax.getURL(@model) url: options.url or Ajax.getURL(@model)
parallel: options.parallel parallel: options.parallel
} }
).done(@recordsResponse) ).done(@recordsResponse(options))
.fail(@failResponse) .fail(@failResponse(options))
fetch: (params = {}, options = {}) -> fetch: (params = {}, options = {}) ->
if id = params.id if id = params.id
@ -153,11 +158,15 @@ class Collection extends Base
# Private # Private
recordsResponse: (data, status, xhr) => recordsResponse: (options) =>
@model.trigger('ajaxSuccess', null, status, xhr) (data, status, xhr, settings) =>
@model.trigger('ajaxSuccess', null, status, xhr, settings)
options.done?.call(@model, settings)
failResponse: (xhr, statusText, error) => failResponse: (options) =>
@model.trigger('ajaxError', null, xhr, statusText, error) (xhr, statusText, error, settings) =>
@model.trigger('ajaxError', null, xhr, statusText, error, settings)
options.fail?.call(@model, settings)
class Singleton extends Base class Singleton extends Base
constructor: (@record) -> constructor: (@record) ->
@ -209,21 +218,27 @@ class Singleton extends Base
# Private # Private
recordResponse: (options = {}) => recordResponse: (options) =>
(data, status, xhr) => (data, status, xhr, settings) =>
if data? and Object.getOwnPropertyNames(data).length and not @record.destroyed
@record.refresh(data, ajax: false)
@record.trigger('ajaxSuccess', @record, @model.fromJSON(data), status, xhr, settings)
options.done?.call(@record, settings)
Ajax.disable => failResponse: (options) =>
unless data is undefined or Object.getOwnPropertyNames(data).length == 0 or @record.destroyed (xhr, statusText, error, settings) =>
# Update with latest data switch settings.type
@record.refresh(data) when 'POST' then @createFailed()
when 'DELETE' then @destroyFailed()
@record.trigger('ajaxError', @record, xhr, statusText, error, settings)
options.fail?.call(@record, settings)
@record.trigger('ajaxSuccess', @record, @model.fromJSON(data), status, xhr) createFailed: ->
options.done?.apply(@record) @record.remove(clear: true)
failResponse: (options = {}) => destroyFailed: ->
(xhr, statusText, error) => @record.destroyed = false
@record.trigger('ajaxError', @record, xhr, statusText, error) @record.constructor.refresh(@record)
options.fail?.apply(@record)
# Ajax endpoint # Ajax endpoint
Model.host = '' Model.host = ''
@ -231,9 +246,9 @@ Model.host = ''
GenerateURL = GenerateURL =
include: (args...) -> include: (args...) ->
args.unshift(encodeURIComponent(@id)) args.unshift(encodeURIComponent(@id))
Ajax.generateURL(@, args...) Ajax.generateURL(this, args...)
extend: (args...) -> extend: (args...) ->
Ajax.generateURL(@, args...) Ajax.generateURL(this, args...)
Include = Include =
ajax: -> new Singleton(this) ajax: -> new Singleton(this)

View file

@ -47,16 +47,16 @@ Events =
stopListening: (obj, events, callback) -> stopListening: (obj, events, callback) ->
if arguments.length is 0 if arguments.length is 0
for listeningTo in [@listeningTo, @listeningToOnce] for listeningTo in [@listeningTo, @listeningToOnce]
continue unless listeningTo continue unless listeningTo?.length
for lt in listeningTo for lt in listeningTo
lt.obj.unbind(lt.ev, lt.callback) lt.obj.unbind(lt.ev, lt.callback)
@listeningTo = undefined @listeningTo = undefined
@listeningToOnce = undefined @listeningToOnce = undefined
else if obj else if obj
events = if events then events.split(' ') else [undefined]
for listeningTo in [@listeningTo, @listeningToOnce] for listeningTo in [@listeningTo, @listeningToOnce]
continue unless listeningTo continue unless listeningTo
events = if events then events.split(' ') else [undefined]
for ev in events for ev in events
for idx in [listeningTo.length-1..0] for idx in [listeningTo.length-1..0]
lt = listeningTo[idx] lt = listeningTo[idx]
@ -236,7 +236,7 @@ class Model extends Module
record.save(options) record.save(options)
@destroy: (id, options) -> @destroy: (id, options) ->
@find(id).destroy(options) @find(id)?.destroy(options)
@change: (callbackOrParams) -> @change: (callbackOrParams) ->
if typeof callbackOrParams is 'function' if typeof callbackOrParams is 'function'
@ -622,7 +622,7 @@ makeArray = (args) ->
Spine = @Spine = {} Spine = @Spine = {}
module?.exports = Spine module?.exports = Spine
Spine.version = '1.5.0' Spine.version = '1.6.1'
Spine.$ = $ Spine.$ = $
Spine.Events = Events Spine.Events = Events
Spine.Log = Log Spine.Log = Log