ampersand-dom-bindings
Advanced tools
Comparing version 3.2.0 to 3.3.0
@@ -50,2 +50,3 @@ var Store = require('key-tree-store'); | ||
var type = binding.type || 'text'; | ||
var isCustomBinding = typeof type === 'function'; | ||
var selector = (function () { | ||
@@ -62,7 +63,14 @@ if (typeof binding.selector === 'string') { | ||
// storage variable for previous if relevant | ||
var previousValue = ''; | ||
var previousValue; | ||
if (type === 'text') { | ||
if (isCustomBinding) { | ||
return function (el, value) { | ||
getMatches(el, selector).forEach(function (match) { | ||
type(match, value, previousValue); | ||
}); | ||
previousValue = value; | ||
}; | ||
} else if (type === 'text') { | ||
return function (el, value) { | ||
getMatches(el, selector).forEach(function (match) { | ||
dom.text(match, value); | ||
@@ -69,0 +77,0 @@ }); |
{ | ||
"name": "ampersand-dom-bindings", | ||
"description": "Takes binding declarations and returns key-tree-store of functions that can be used to apply those bindings.", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"author": "'Henrik Joreteg' <henrik@andyet.net>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -443,2 +443,58 @@ var test = require('tape'); | ||
// Custom bindings | ||
test('custom binding', function (t) { | ||
var el = getEl('<span class="thing"></span>'); | ||
var custom = function (bindingEl, value, previous) { | ||
var msg = value + ' is the new value.'; | ||
msg += typeof previous !== 'undefined' ? ' previous value was ' + previous + '.' : ''; | ||
dom.text(bindingEl, msg); | ||
}; | ||
var bindings = domBindings({ | ||
'model': { | ||
type: custom, | ||
selector: '.thing' | ||
} | ||
}); | ||
t.equal(el.firstChild.textContent, ''); | ||
bindings.run('model', null, el, 'hello'); | ||
t.equal(el.firstChild.textContent, 'hello is the new value.'); | ||
bindings.run('model', null, el, 'goodbye'); | ||
t.equal(el.firstChild.textContent, 'goodbye is the new value. previous value was hello.'); | ||
bindings.run('model', null, el, ''); | ||
t.equal(el.firstChild.textContent, ' is the new value. previous value was goodbye.'); | ||
bindings.run('model', null, el, 'goodbye'); | ||
t.equal(el.firstChild.textContent, 'goodbye is the new value. previous value was .'); | ||
t.end(); | ||
}); | ||
//Bad type is an error | ||
test('Errors on a bad type', function (t) { | ||
function bindings(type) { | ||
return function () { | ||
domBindings({ | ||
'model': { | ||
type: type, | ||
selector: '.thing' | ||
} | ||
}); | ||
}; | ||
} | ||
function errMsg(msg) { | ||
return new RegExp(('no such binding type: ' + msg).replace(/[\[\]]/g, '\\$&')); | ||
} | ||
t.throws(bindings('not-a-type'), errMsg('not-a-type')); | ||
t.throws(bindings({}), errMsg({})); | ||
t.throws(bindings([]), errMsg([])); | ||
t.end(); | ||
}); | ||
// TODO: tests for toggle | ||
@@ -445,0 +501,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33281
587