Config panel handles binding for Editors

This commit is contained in:
probablycorey
2013-04-24 15:57:42 -07:00
committed by Corey Johnson & Kevin Sawicki
parent 1b4fbdb065
commit cedea831f7
2 changed files with 48 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
ConfigPanel = require 'config-panel'
Editor = require 'editor'
describe "ConfigPanel", ->
it "automatically binds named input fields to their corresponding config keys", ->
@@ -40,3 +41,34 @@ describe "ConfigPanel", ->
panel.stringInput.val('moo').change()
expect(config.get('foo.string')).toBe 'moo'
it "automatically binds named editors to their corresponding config keys", ->
class TestPanel extends ConfigPanel
@content: ->
@div =>
@subview 'intEditor', new Editor(mini: true, attributes: { id: 'foo.int', type: 'int' })
@subview 'floatEditor', new Editor(mini: true, attributes: { id: 'foo.float', type: 'float' })
@subview 'stringEditor', new Editor(mini: true, attributes: { id: 'foo.string', type: 'string' })
config.set('foo.int', 1)
config.set('foo.float', 1.1)
config.set('foo.string', 'I think therefore I am.')
panel = new TestPanel
expect(panel.intEditor.getText()).toBe '1'
expect(panel.floatEditor.getText()).toBe '1.1'
expect(panel.stringEditor.getText()).toBe 'I think therefore I am.'
config.set('foo.int', 2)
config.set('foo.float', 2.2)
config.set('foo.string', 'We are what we think.')
expect(panel.intEditor.getText()).toBe '2'
expect(panel.floatEditor.getText()).toBe '2.2'
expect(panel.stringEditor.getText()).toBe 'We are what we think.'
panel.intEditor.setText('3')
panel.floatEditor.setText('3.3')
panel.stringEditor.setText('All limitations are self imposed.')
window.advanceClock(10000) # wait for contents-modified to be triggered
expect(config.get('foo.int')).toBe 3
expect(config.get('foo.float')).toBe 3.3
expect(config.get('foo.string')).toBe 'All limitations are self imposed.'

View File

@@ -5,6 +5,7 @@ module.exports =
class ConfigPanel extends View
initialize: ->
@bindFormFields()
@bindEditors()
bindFormFields: ->
for input in @find('input[id]').toArray()
@@ -28,3 +29,18 @@ class ConfigPanel extends View
!!input.attr('checked')
else
value
bindEditors: ->
for editor in @find('.editor[id]').views()
do (editor) =>
name = editor.attr('id')
type = editor.attr('type')
@observeConfig name, (value) ->
editor.setText(value.toString())
editor.getBuffer().on 'contents-modified', ->
value = editor.getText()
if type == 'int' then value = parseInt(value)
if type == 'float' then value = parseFloat(value)
config.set name, value