Skip to content

Commit fa736bb

Browse files
committed
feat: support DOM driven value population
As per discussion here: https://remysharp.com/2015/06/02/bind#comment-2204933433 now if the value in the bind object is `null` it'll populate it from the DOM (if one is provided).
1 parent 59d12c3 commit fa736bb

File tree

7 files changed

+76
-6
lines changed

7 files changed

+76
-6
lines changed

.jscsrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"preset": "node-style-guide",
3+
"requireCapitalizedComments": null,
4+
"requireSpacesInAnonymousFunctionExpression": {
5+
"beforeOpeningCurlyBrace": true,
6+
"beforeOpeningRoundBrace": true
7+
},
8+
"disallowSpacesInNamedFunctionExpression": {
9+
"beforeOpeningRoundBrace": true
10+
},
11+
"excludeFiles": ["node_modules/**"],
12+
"disallowSpacesInFunction": null
13+
}

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = function (config) {
6565
// - PhantomJS
6666
// - IE (only Windows)
6767
// CLI --browsers Chrome,Firefox,Safari
68-
browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],
68+
browsers: [process.env.TRAVIS ? 'Firefox' : 'ChromeCanary'],
6969

7070
// If browser does not capture in given timeout [ms], kill it
7171
// CLI --capture-timeout 5000

lib/bind.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ var Bind = (function Bind(global) {
168168
// the node type, and either sets the input.value or
169169
// element.innerHTML to the value
170170
var valueSetters = ['SELECT', 'INPUT', 'PROGRESS'];
171+
172+
if (value === null || value === undefined) {
173+
if (valueSetters.indexOf(elements[0].nodeName) !== -1) {
174+
value = parse(elements[0].value);
175+
} else {
176+
value = parse(elements[0].innerHTML);
177+
}
178+
}
179+
180+
171181
var oldCallback = callback;
172182
callback = function (value) {
173183
// make it a live selection
@@ -311,6 +321,10 @@ var Bind = (function Bind(global) {
311321
path.reduce(function (prev, curr, i) {
312322
if (prev && prev[curr] && curr) {
313323
instance = instance[curr];
324+
325+
if (instance === null || instance === undefined) {
326+
return prev[curr] || {};
327+
}
314328
if (typeof prev[curr].__callback === 'function') {
315329
var v = i === path.length - 1 ? value : instance;
316330
if (instance.__dirty) {

test/dom-values.browser.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
var assert = require('assert');
3+
var html = require('./html');
4+
var Bind = require('../');
5+
6+
/*globals describe, assert, beforeEach, Bind, it */
7+
describe('DOM values informing bind', function () {
8+
var data;
9+
10+
beforeEach(function () {
11+
html(window.__html__['test/dom-values.html']);
12+
13+
data = new Bind({
14+
score: undefined,
15+
price: null,
16+
}, {
17+
score: '.score',
18+
price: {
19+
dom: '.price',
20+
parse: function (v) {
21+
return parseFloat(v.replace(/£/, ''));
22+
},
23+
transform: function (v) {
24+
return '£' + v;
25+
},
26+
},
27+
});
28+
});
29+
30+
it('should find selectors', function () {
31+
var nodes = document.querySelectorAll('.score');
32+
assert.equal(nodes.length, 2, 'expected to find two .score els');
33+
});
34+
35+
it('should update bind values based on DOM', function () {
36+
assert.equal(data.score, '10', 'bind value is correct: ' + data.score);
37+
assert.ok(data.price === 100, 'price is right: ' + data.price);
38+
});
39+
});

test/dom-values.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<span class="score">10</span>
2+
<progress class="score" min=0 max=100>
3+
4+
<p class="price">£100</p>

test/export.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ var sinon = require('sinon');
33
var assert = require('assert');
44
var Bind = require('../');
55

6-
/*globals describe, assert, beforeEach, Bind, sinon, it */
6+
/* globals describe, assert, Bind, sinon, it */
77
describe('export', function () {
8-
var data;
9-
108
it('returns vanilla object', function () {
119
var spy = sinon.spy();
1210
var o = {
@@ -16,7 +14,7 @@ describe('export', function () {
1614
location: 'house',
1715
},
1816
};
19-
var data = Bind(o, {
17+
var data = new Bind(o, {
2018
name: spy,
2119
});
2220

test/two-way.browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ describe('two way data bind', function () {
1414
data = Bind({
1515
score: 10,
1616
name: 'Julie',
17+
body: 'Some really long\nstatement about stuff',
1718
}, {
1819
score: '.score',
1920
name: '.name',
21+
body: 'textarea.body',
2022
});
2123
});
2224

2325
it('should find selectors', function () {
24-
var target = data.score = 10;
2526
var nodes = document.querySelectorAll('.score');
27+
assert.equal(nodes.length, 3, 'found DOM nodes: ' + nodes.length);
2628
});
2729

2830
it('should update DOM to data values', function () {

0 commit comments

Comments
 (0)