Skip to content

Implement props and bindings #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions ampersand-checkbox-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ module.exports = View.extend({

// settings
this.el = opts.el;
this.name = opts.name;
this.startingValue = !!opts.value;
this.value = this.startingValue;
this.label = opts.label || opts.name;
this.required = (typeof opts.required === 'boolean') ? opts.required : false;
this.disabled = (typeof opts.disabled === 'boolean') ? opts.disabled : false;
this.validClass = opts.validClass || 'input-valid';
this.invalidClass = opts.invalidClass || 'input-invalid';
this.requiredMessage = opts.requiredMessage || 'This box must be checked.';
Expand All @@ -37,6 +33,51 @@ module.exports = View.extend({

this.setValue(this.value);
},

props: {
name: {
type: 'string',
required: true
},
value: {
type: 'boolean',
default: false
},
label: {
type: 'string'
},
required: {
type: 'boolean',
default: false
},
disabled: {
type: 'boolean',
default: false
}
},

bindings: {
disabled: {
type: 'booleanAttribute',
selector: 'input',
name: 'disabled'
},
value: {
type: 'booleanAttribute',
selector: 'input',
name: 'checked'
},
name: {
type: 'attribute',
selector: 'input',
name: 'name'
},
label: {
type: 'text',
hook: 'label'
}
},

clear: function () {
return this.setValue(false);
},
Expand All @@ -61,10 +102,8 @@ module.exports = View.extend({
this.input.addEventListener('change', this.handleInputEvent, false);

this.setMessage(this.message);
this.input.checked = !!this.value;
this.input.disabled = this.disabled;
this.input.name = this.name;
this.labelEl.textContent = this.label;

return this;
},
// handle input events and show appropriate errors
handleInputEvent: function () {
Expand Down Expand Up @@ -95,11 +134,13 @@ module.exports = View.extend({
}
},
setValue: function (value) {
if (this.input) this.input.checked = !!value;
this.value = !!value;

return this.handleInputEvent();
},
beforeSubmit: function () {
this.shouldValidate = true;

return this.handleInputEvent();
},
test: function () {
Expand Down
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ test('basics', function (t) {
t.strictEqual(view.disabled, true, 'disabled should be true');
t.strictEqual(view.input.disabled, true, 'disabled should be true');

view.disabled = false;

t.strictEqual(view.disabled, false, 'disabled should now be false');
t.strictEqual(view.input.disabled, false, 'disabled should now be false');

//test throw when lacking name
t.throws(function() {
view = new CheckboxView({
Expand Down