southbay
Version:
Cocoa-inspired key-value coding, binding, and observing for node
48 lines (46 loc) • 1.64 kB
JavaScript
var $ = require('southbay'),
vows = require('vows'),
assert = require('assert'),
o = $.Base.extend({
nextLevelChange: function(p, o, c) {
this.setValue(c).forKey('changeDict');
}
}),
o2 = $.Base.extend({
jeanShortsChange: function(p, o, c) {
this.setValue(p).forKey('observedPath');
}
}),
O = new o(),
// passes in an initial value to the classes data model
O2 = new o2({nextLevel: 'biodiesel'});
vows.describe('Southbay Key-Value Observing').addBatch({
'A KVO Object': {
topic: O,
'implements key-value coding': function(kvo) {
kvo.setValue('readyMade').forKey('mustache');
assert.equal(O.valueForKey('mustache'), 'readyMade');
},
'can use keypaths': function(kvo) {
kvo.setValue(true).forKeyPath('apparel.jeanShorts');
assert.equal(O.valueForKeyPath('apparel.jeanShorts'), true);
},
'should be able to register an observer': function(kvo) {
kvo.addObserver(O2).forKeyPath('apparel.jeanShorts').options(
$.NEW).context('jeanShortsChange');
assert.isObject(kvo._observers_['apparel.jeanShorts'][0].object);
},
'should notify an observer': function(kvo) {
kvo.setValue(false).forKeyPath('apparel.jeanShorts');
assert.equal(O2.valueForKey('observedPath'), 'apparel.jeanShorts');
},
'can recieve both old and new values via change dictionary': function(kvo) {
O2.addObserver(kvo).forKeyPath('nextLevel').options(
$.OLD, $.NEW).context('nextLevelChange');
O2.setValue('food truck').forKey('nextLevel');
var dict = kvo.valueForKey('changeDict'),
ary = [dict.Old, dict.New];
assert.deepEqual(ary, ['biodiesel', 'food truck']);
}
}
}).export(module);