Skip to content
Browse files

child views listen for errors

  • Loading branch information...
1 parent 206fa47 commit 6c6973dd013949ab27cc27d532f174cb8147e7b2 Chris Nelson committed Jun 5, 2012
View
10 lib/assets/javascripts/views/form_field_view.coffee
@@ -6,6 +6,7 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
@label = options.label
@parentView = options.parentView
@parentView.on "rendered", => @afterParentRender()
+ @model.on "error", (model, errors) => @displayErrors(errors)
afterParentRender: ->
@setElement(@parentView.$("[data-view-id=#{@cid}]"))
@@ -15,9 +16,12 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
super
@$el.addClass "control-group"
- displayErrors: (messages) ->
- @$el.addClass "error"
- @$el.append "<span class='help-inline'>#{message}</span>" for message in messages
+ displayErrors: (errors) ->
+ errors = errors.errors if errors.errors
+ if errors?[@field]
+ messages = errors[@field]
+ @$el.addClass "error"
+ @$el.append "<span class='help-inline'>#{message}</span>" for message in messages
clearErrors: ->
@$el.removeClass "error"
View
15 lib/assets/javascripts/views/form_view.coffee
@@ -3,17 +3,7 @@ class Backtastic.Views.FormView extends Backtastic.View
constructor: ->
super
@fieldViews = {}
-
- clearErrors: ->
- fieldView.clearErrors() for field, fieldView of @fieldViews
-
- displayErrors: (response)->
- @$("input[type='submit']").removeAttr("disabled")
- errors = JSON.parse(response.responseText)
- errors = errors.errors if errors.errors #rails does it this way
- for field, errorMessages of errors
- @fieldViews[field]?.displayErrors(errorMessages)
-
+
fieldView: (fieldViewClass, options) ->
fieldView = new fieldViewClass _.extend options,
parentView: @
@@ -32,7 +22,6 @@ class Backtastic.Views.FormView extends Backtastic.View
save: (event)->
@$("input[type='submit']").attr("disabled", "disabled")
- @clearErrors()
event.preventDefault()
- @model.on "error", (model, response) => @displayErrors(response)
+ @model.on "error", (model, errors) => @$("input[type='submit']").removeAttr("disabled")
@model.save @$("form").serializeObject()

0 comments on commit 6c6973d

Please sign in to comment.
Something went wrong with that request. Please try again.