Support setting number fields to 0

Previously entering 0 would end up as '0' in the config
value instead of as a numeric value.
This commit is contained in:
Kevin Sawicki
2013-04-30 23:10:22 -07:00
parent 1afe4f0cd9
commit 536beb0d40
2 changed files with 18 additions and 2 deletions

View File

@@ -39,6 +39,12 @@ describe "ConfigPanel", ->
panel.floatInput.val('90.2').change()
expect(config.get('foo.float')).toBe 90.2
panel.intInput.val('0').change()
expect(config.get('foo.int')).toBe 0
panel.floatInput.val('0').change()
expect(config.get('foo.float')).toBe 0
panel.stringInput.val('moo').change()
expect(config.get('foo.string')).toBe 'moo'
@@ -102,6 +108,12 @@ describe "ConfigPanel", ->
expect(config.get('foo.float')).toBe undefined
expect(config.get('foo.string')).toBe undefined
panel.intEditor.setText('0')
panel.floatEditor.setText('0')
window.advanceClock(10000) # wait for contents-modified to be triggered
expect(config.get('foo.int')).toBe 0
expect(config.get('foo.float')).toBe 0
it "does not save the config value until it has been changed to a new value", ->
class TestPanel extends ConfigPanel
@content: ->

View File

@@ -32,8 +32,12 @@ class ConfigPanel extends View
parseValue: (type, value) ->
switch type
when 'int' then value = parseInt(value) or value
when 'float' then value = parseFloat(value) or value
when 'int'
intValue = parseInt(value)
value = intValue unless isNaN(intValue)
when 'float'
floatValue = parseFloat(value)
value = floatValue unless isNaN(floatValue)
value = undefined if value == ''
value