Skip to content

Instantly share code, notes, and snippets.

@multiplier
Created November 24, 2012 21:57
Show Gist options
  • Save multiplier/4141546 to your computer and use it in GitHub Desktop.
Save multiplier/4141546 to your computer and use it in GitHub Desktop.
backbonejserror
class Trainer.Views.LessonsIndex extends Backbone.View
template: JST['lessons/index']
initialize: ->
@collection.on('reset', @render, this);
@collection.on('add', @render, this);
render: ->
$(@el).html(@template(lessons: @collection))
this
events:
'submit #new_lesson': 'createLesson'
createLesson: (event) ->
event.preventDefault()
attributes=
title: $('#new_lesson_title').val(),
description: $('#new_lesson_description').val()
@collection.create attributes,
success: -> $('#new_lesson')[0].reset()
error: @handleError
handleError: (lesson, response) ->
if response.status == 422
errors = $.parseJSON(response.responseText).errors
for attribute, messages of errors
alert "#{attribute} #{message}" for message in messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment