Add support for input[type="checkbox"]

This commit is contained in:
Joe Cheng
2012-06-21 19:38:43 -07:00
parent 2990504c8b
commit 51e66f8aee
4 changed files with 19 additions and 6 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
vendor/ruby
\.bundle/
\.bundle/
\.DS_Store

View File

@@ -4,7 +4,9 @@ require 'digest/md5'
shinyapp = ShinyApp.new
input1 = React::ObservableValue.new { shinyapp.session.get('input1') }
input1 = React::ObservableValue.new {
(shinyapp.session.get('input1') || '') + (shinyapp.session.get('addnewline') ? "\n" : '')
}
shinyapp.define_output('md5_hash') do
Digest::MD5.hexdigest(input1.value)

View File

@@ -10,6 +10,7 @@
<p>
<label>Input:</label><br />
<input name="input1" value="Hello World!"/>
<input type="checkbox" name="addnewline" checked="checked"/> Append newline
</p>
<p>

View File

@@ -108,20 +108,29 @@
shinyapp.bind(this.id, new LiveTextBinding(this));
});
function elementToValue(el) {
if (el.type == 'checkbox')
return el.checked ? true : false;
else
return $(el).val();
}
var initialValues = {};
$('input').each(function() {
var input = this;
var name = input.name;
var value = $(input).val();
var value = elementToValue(input);
// TODO: validate name is non-blank, and no duplicates
// TODO: If submit button is present, don't send anything
// until submit button is pressed
initialValues[name] = value;
$(input).keyup(function() {
var onChange = function() {
var data = {};
data[name] = $(input).val();
data[name] = elementToValue(input);
shinyapp.sendInput(data);
});
};
$(input).keyup(onChange);
$(input).change(onChange);
});
shinyapp.connect(initialValues);