Skip to content

Instantly share code, notes, and snippets.

@kixxauth
Created September 9, 2014 17:32
Show Gist options
  • Save kixxauth/e41e6d0ae9e15972fcd5 to your computer and use it in GitHub Desktop.
Save kixxauth/e41e6d0ae9e15972fcd5 to your computer and use it in GitHub Desktop.
model.save() with promise
save: (data, options = {}) ->
entity = @
isNew = @isNew()
success = _.bind(@saveSuccess, @, isNew, options.collection, options.callback)
error = _.bind(@saveError, @)
promise = new Promise (resolve, reject) ->
_.defaults options,
wait: true
success: ->
success.apply(@, arguments)
resolve(entity)
error: (entity, response) ->
App.log.error('API Sync Error in loadRelations():', response)
error.apply(@, arguments)
#reject(new Error('API Sync Error in relations'))
err = new Error('API Sync Error ')
err.response = response
reject(err)
@unset('_errors')
super(data, options)
return promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment