0

I just watched the backbone.js railscasts and Im not getting the error alert when the name is not present but do get an error on the server side.

im using rails 3.2.8 and backbone-on-rails 0.9.2.3

In the network console it does state that theres an error with the api/entries, and in the response theres the errors key, but still no alert when I click add with nothing on the form.

This is my code... please help me figure out what could be wrong!

class Raffler.Views.EntriesIndex extends Backbone.View

  template: JST['entries/index']

  events:
    'submit #new_entry': 'createEntry'
    'click #draw': 'drawWinner'

  initialize: ->
    @collection.on('reset', @render)
    @collection.on('add', @appendEntry)

  render: =>
    $(@el).html(@template())
    @collection.each(@appendEntry)
    this

  appendEntry: (entry) =>
    view = new Raffler.Views.Entry(model: entry)
    @$('#entries').append(view.render().el)

  drawWinner: (event) ->
    event.preventDefault()
    @collection.drawWinner()

  createEntry: (event) ->
    event.preventDefault()
    attributes = name: $('#new_entry_name').val()
    @collection.create attributes,
      wait: true
      success: -> $('#new_entry')[0].reset()
      error: @handleError

    handleError: (entry, response) ->
      if response.status == 422
        errors = $.parseJSON(response.responseText).errors
        for attribute, messages of errors
          alert "#{attribute} #{message}" for message in messages

Thank you!

kangax
  • 38,898
  • 13
  • 99
  • 135
maumercado
  • 1,453
  • 4
  • 23
  • 47

1 Answers1

0

Your indentation is wrong so you don't have a @handleError, instead your createEntry method is returning an object like this:

{ handleError: (entry, response) -> ... }

So when you try to use @handleError as an error callback:

@collection.create attributes,
  wait: true
  success: -> ...
  error: @handleError # <----------- Here

you're really saying:

@collection.create attributes,
  wait: true
  success: -> ...
  error: undefined

and setting your error callback to undefined doesn't do anything useful.

Pull handleError back towards the left margin two spaces and you might have better luck.

mu is too short
  • 426,620
  • 70
  • 833
  • 800