Skip to content

Instantly share code, notes, and snippets.

@sandersch
Created February 11, 2012 19:06
Show Gist options
  • Save sandersch/1803638 to your computer and use it in GitHub Desktop.
Save sandersch/1803638 to your computer and use it in GitHub Desktop.
Mixin trouble with Spine.js
class AjaxHelpers
unbind_ajax_handlers: (item) ->
item.unbind('ajaxSuccess')
item.unbind('ajaxError')
ajax_error_handler: (item, xhr) ->
if xhr.responseText
alert("#{key.split('_').join(' ')} #{value}") for key, value of jQuery.parseJSON(xhr.responseText)
else
alert("There was a problem submitting this update to the server.")
@unbind_ajax_handlers(item)
class Edit extends Spine.Controller
@include AjaxHelpers
submit: (e) =>
e.preventDefault()
@errors.empty()
previous = @item.dup()
if @item.fromForm(e.target).save()
@stack.new.active()
@item.bind 'ajaxSuccess', (sent, record, statusText, xhr) =>
$('.flashMessage p').stop().text("Entry saved").fadeOut(2500)
@unbind_ajax_handlers(@item)
@item.bind 'ajaxError', (record, xhr, statusText, body) =>
@ajax_error_handler(@item, xhr)
@item.updateAttributes(rating: previous.rating, timestamp: previous.timestamp)
@item.save()
Spine.Ajax.disable ->
previous.destroy()
else
error_text = @item.fromForm(e.target).validate()
@errors.append($('<li/>', {html: error_text}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment