Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Improve the validation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RStankov committed Jan 2, 2012
1 parent c95fd0c commit abe05f5
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions assets/javascripts/application.js.coffee
Expand Up @@ -114,23 +114,18 @@ class PinView.Form extends App.View
template: App.templateFor('pin-form')
events:
'click [data-cancel]': 'cancel'
'click [data-save]': 'save'
'click [data-save]': 'save'

save: ->
titleInput = @find('[data-attribute="title"]')
bodyInput = @find('[data-attribute="body"]')
initialize: ->
@model.bind 'error', @handlesErrors, this

save: ->
attributes =
title: titleInput.val()
body: bodyInput.val()
title: @find('[data-attribute="title"]').val()
body: @find('[data-attribute="body"]').val()

errors = @model.validate(attributes: attributes)
unless errors
@model.set(attributes)
if @model.set(attributes)
@model.show()
else
titleInput.toggleClass 'error', 'title' of errors
bodyInput.toggleClass 'error', 'body' of errors

cancel: ->
@model.cancel()
Expand All @@ -143,10 +138,14 @@ class PinView.Form extends App.View
$(@el).show()
@find(':input:first').focus()

handlesErrors: (model, errors) ->
@find('[data-attribute="title"]').toggleClass 'error', 'title' of errors
@find('[data-attribute="body"]').toggleClass 'error', 'body' of errors

class PinView.Display extends App.View
template: App.templateFor('pin-display')
events:
'click [data-edit]': 'edit'
'click [data-edit]': 'edit'
'click [data-delete]': 'destroy'

edit: ->
Expand Down

0 comments on commit abe05f5

Please sign in to comment.