Skip to content

Commit

Permalink
QUICKFIX - don't enable all els when save fails
Browse files Browse the repository at this point in the history
Change-Id: If4b0c16d9f2b2e92d423474f36cca89aaf5e239b
  • Loading branch information
Chris McBride committed Aug 21, 2013
1 parent a8acbea commit a9493ae
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
16 changes: 9 additions & 7 deletions lib/lib/forms.coffee
Expand Up @@ -27,8 +27,11 @@ class FormView extends View

save: (e) ->
e?.preventDefault()
@_hideErrors()
elements = @_disableForm()

local = new Deferred
local.always => @_enableForm()
local.always => @_enableElements(elements)

onError = (errors) =>
local.reject(errors)
Expand All @@ -41,9 +44,6 @@ class FormView extends View
@deferred.resolve(@model)
@trigger('saved')

@_hideErrors()
@_disableForm()

try
update = @_getUpdate()
@model.save(update, quiet: true)
Expand Down Expand Up @@ -88,11 +88,13 @@ class FormView extends View

parent.addClass('error')

_enableForm: ->
@$el.find('input').prop('disabled', false)
_enableElements: (elements) ->
elements.prop('disabled', false)

_disableForm: ->
@$el.find('input').prop('disabled', true)
enabledElements = @$el.find(':input:not(:disabled)')
enabledElements.prop('disabled', true)
enabledElements

_populateForm: ->
for name, field of _.result(this, 'fields')
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "quips",
"version": "0.2.8",
"version": "0.2.9",
"dependencies": {
"coffee-script": "1.6.x",
"jqueryify": "1.9.1",
Expand Down
4 changes: 4 additions & 0 deletions test/lib/form_view_spec_template.haml
Expand Up @@ -34,3 +34,7 @@
.controls
.other-container
%input{name: 'youngerThanMark', type: 'text'}

%p
%label Read Only Field
%input{name: 'readOnlyField', type: 'text', disabled: true}
14 changes: 14 additions & 0 deletions test/lib/forms_spec.coffee
Expand Up @@ -19,6 +19,7 @@ class TestForm extends forms.FormView
gender: forms.stringField
olderThanAndrew: forms.boolField
youngerThanMark: forms.intField
readOnlyField: forms.stringField


describe 'Form View', ->
Expand Down Expand Up @@ -199,6 +200,19 @@ describe 'Form View', ->
expect(errors['age']).to.have.length 1
expect(errors['age'][0]).to.equal 'Invalid Number'

it 'should disable the form until the save is complete', (done) ->
test.when 'POST', '/lils/billy', (req) =>
expect(@form.$el.find(':input:disabled')).to.have.length 9
status: 204

expect(@form.$el.find(':input:disabled').length).to.equal 1
@form.on('saved', (->
# originally disabled element is preserved
expect(@form.$el.find(':input:disabled').length).to.equal 1
done()), this)

@form.save()

describe 'and the input is invalid', ->

beforeEach ->
Expand Down
15 changes: 9 additions & 6 deletions test/setup.coffee
Expand Up @@ -55,12 +55,15 @@ module.exports =

for [method, url, respond] in @patterns
if method is request.method and url is request.url
resp = respond(request) or {}
resp.status or= 200
resp.body or= ''

request.receive resp.status, resp.body
handed = true
try
resp = respond(request) or {}
resp.status or= 200
resp.body or= ''

request.receive resp.status, resp.body
handed = true
catch e
console.log(e.message)

unless handed
request.receive 404, 'Not Found'

0 comments on commit a9493ae

Please sign in to comment.