southbay
Version:
Cocoa-inspired key-value coding, binding, and observing for node
72 lines (70 loc) • 2.01 kB
JavaScript
var $ = require('southbay'),
vows = require('vows'),
assert = require('assert'),
// Bindables can use any function as a 'transformer' to adjust
// the value passed to them before setting
megatron = function(str) {return str + ' Love, Megatron. xoxo';},
B1 = new $.Base(),
b2 = $.Base.extend({
text: function(txt) {
this.setValue(txt).forKey('text');
},
constructor: function() {
$.Base.call(this);
this.bind('text').toObject(B1).withKeyPath('value').options(0);
}
}),
B2 = new b2(),
b3 = $.Base.extend({
text: function(txt) {
this.setValue(txt).forKey('text');
},
constructor: function() {
$.Base.call(this);
this.bind('text').toObject(B1).withKeyPath('value').options({
transformer: megatron,
placeholder: 'plotting evil...'
});
}
}),
B3 = new b3();
vows.describe('Southbay Key-Value-Binding').addBatch({
'A Southbay Object with a `text` binding:': {
topic: B2,
'does not initially have a value': function(kvb) {
assert.isUndefined(kvb.valueForKey('text'));
}
},
'Another Southbay Object with a `text` binding:': {
topic: B3,
'also does not initially have a value': function(kvb) {
assert.isUndefined(kvb.valueForKey('text'));
}
},
'Properly implements a binding': {
topic: B2,
'by synchronizing its property with that of another': function(kvb) {
B1.setValue('hello binding.').forKey('value');
assert.equal(B2.valueForKey('text'), 'hello binding.');
}
},
'Will use a declared transformer': {
topic: B3,
'on the passed in value': function(kvb) {
assert.equal(kvb.valueForKey('text'), 'hello binding. Love, Megatron. xoxo');
}
},
'Can allow a falsy value': {
topic: B2,
'if passed one and no placeholder declared': function(kvb) {
B1.setValue('').forKey('value');
assert.equal(B2.valueForKey('text'), '');
}
},
'Will insert a placeholder': {
topic: B3,
'for falsy if one is declared': function(kvb) {
assert.equal(B3.valueForKey('text'), 'plotting evil...');
}
}
}).export(module);