public
Last active

Knockout observables with ECMAScript 5 Properties

  • Download Gist
ko-es5.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
(function(factory, global) {
if (global.define && global.define.amd) {
global.define(['ko'], factory);
} else {
factory(global.ko);
}
})(function(ko) {
 
var deepObservifyArray = function(arr, deep) {
for (var i = 0, len = arr.length; i < len; i++) {
// TODO circular reference protection
if (arr[i] != null && typeof arr[i] == 'object') {
if (deep && Array.isArray(arr[i])) {
deepObservifyArray(arr[i], deep);
} else {
arr[i] = ko.observableModel(arr[i], deep);
}
}
}
},
defineProperty = function(type, obj, prop, def, deep) {
if (obj == null || typeof obj != 'object' || typeof prop != 'string') {
throw new Error('invalid arguments passed');
}
 
if (Object.prototype.toString.call(def) === '[object Array]' && type === 'observable') {
type = 'observableArray';
}
 
if (deep !== false && (def != null && typeof def == 'object')) {
if (Array.isArray(def)) {
deepObservifyArray(def, deep);
} else {
def = ko.observableModel(def, deep);
}
}
 
var obv = ko[type](def);
Object.defineProperty(obj, prop, {
set: function(value) { obv(value) },
get: function() { return obv() },
enumerable: true,
configurable: true
});
 
Object.defineProperty(obj, '_' + prop, {
get: function() { return obv },
enumerable: false
});
};
 
ko.utils.defineObservableProperty = defineProperty.bind(null, 'observable');
ko.utils.defineComputedProperty = defineProperty.bind(null, 'computed');
 
ko.observableModel = function(defaults, deep) {
var model = {}, def;
for (var prop in defaults) {
if (defaults.hasOwnProperty(prop)) {
def = defaults[prop];
if (!def || !ko.isSubscribable(def)) {
ko.utils.defineObservableProperty(model, prop, def, deep);
} else {
model[prop] = def;
}
}
}
return model;
};
 
ko.observableArrayModel = function(arr, deep) {
var copy = arr.slice(0);
deepObservifyArray(copy, deep);
return ko.observableArray(copy);
};
 
}, this);
tests.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ko-es5.js tests</title>
<link rel="stylesheet" href="./lib/mocha.css" />
 
<script src="./lib/knockout-2.1.0.debug.js"></script>
<script src="./lib/chai.js"></script>
<script src="./lib/mocha.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>var should = chai.should(); var expect = chai.expect; mocha.setup('bdd')</script>
 
<script src="ko-es5.js"></script>
 
<script>
describe('es5-observable', function() {
describe('defineObservableProperty', function() {
it('should define an enumerable property', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'name');
 
var descriptor = Object.getOwnPropertyDescriptor(foo, 'name');
descriptor.enumerable.should.be.true;
descriptor.configurable.should.be.true;
descriptor.get.should.be.a('function');
descriptor.set.should.be.a('function');
});
 
it('should set a default if provided', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'name', 'Bob');
 
foo.name.should.equal('Bob');
});
 
it('should define an non-enumerable property to access the observable', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'name');
 
var descriptor = Object.getOwnPropertyDescriptor(foo, '_name');
descriptor.enumerable.should.be.false;
descriptor.configurable.should.be.false;
descriptor.get.should.be.a('function');
expect(descriptor.set).to.equal(undefined);
 
ko.isObservable(foo._name).should.be.true;
});
 
it('should special case arrays and create an observableArray', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'friends', []);
 
ko.isObservable(foo._friends).should.be.true;
foo._friends.push.should.be.a('function');
});
 
it('should deeply observify objects by default', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'friends', [{
name: 'Bob',
titles: ['mr', 'sir']
}]);
 
ko.isObservable(foo._friends).should.be.true;
foo._friends.push.should.be.a('function');
 
ko.isObservable(foo.friends[0]._name).should.be.true;
ko.isObservable(foo.friends[0]._titles).should.be.true;
});
 
it('should not deeply observify objects if requested', function() {
var foo = {};
 
ko.utils.defineObservableProperty(foo, 'friends', [{
name: 'Bob',
kids: {
sally: 10,
sue: 5
}
}], false);
 
ko.isObservable(foo.friends[0]._kids).should.be.false;
foo.friends[0].kids.should.not.haveOwnProperty('_sally');
foo.friends[0].kids.sally.should.equal(10);
});
});
 
describe('observableModel', function() {
it('should create an object with observable properties', function() {
var obj = ko.observableModel({
name: 'Bob',
age: undefined
});
 
var nameDescriptor = Object.getOwnPropertyDescriptor(obj, 'name');
nameDescriptor.get.should.be.a('function');
nameDescriptor.set.should.be.a('function');
nameDescriptor.enumerable.should.be.true;
 
var ageDescriptor = Object.getOwnPropertyDescriptor(obj, 'age');
ageDescriptor.get.should.be.a('function');
ageDescriptor.set.should.be.a('function');
ageDescriptor.enumerable.should.be.true;
 
obj.name.should.equal('Bob');
expect(obj.age).to.equal(undefined);
});
 
it('should create observable properties deeply', function() {
var obj = ko.observableModel({
name: 'Bob',
job: {
title: undefined,
company: 'acme'
}
});
 
var jobTitleDescriptor = Object.getOwnPropertyDescriptor(obj.job, 'title');
jobTitleDescriptor.get.should.be.a('function');
jobTitleDescriptor.set.should.be.a('function');
jobTitleDescriptor.enumerable.should.be.true;
 
var jobDescriptor = Object.getOwnPropertyDescriptor(obj, 'job');
jobDescriptor.get.should.be.a('function');
jobDescriptor.set.should.be.a('function');
 
obj.name.should.equal('Bob');
obj.job.company.should.equal('acme');
});
 
it('should not create observable properties deeply if requested', function() {
var obj = ko.observableModel({
name: 'Bob',
job: {
title: undefined,
company: 'acme'
}
}, false);
 
var jobTitleDescriptor = Object.getOwnPropertyDescriptor(obj.job, 'title');
expect(jobTitleDescriptor.set).to.equal(undefined);
expect(jobTitleDescriptor.get).to.equal(undefined);
 
var jobDescriptor = Object.getOwnPropertyDescriptor(obj, 'job');
jobDescriptor.get.should.be.a('function');
jobDescriptor.set.should.be.a('function');
 
obj.name.should.equal('Bob');
obj.job.company.should.equal('acme');
});
 
it('should special case arrays and create an observableArray', function() {
var obj = ko.observableModel({
name: 'Bob',
friends: ['Jane', 'Jill']
});
 
ko.isObservable(obj._friends).should.be.true;
obj._friends.push.should.be.a('function');
obj.friends[0].should.equal('Jane');
});
 
it('should not define properties for subscribables', function() {
var obj = ko.observableModel({
name: 'Bob',
friends: ko.observableArray()
});
 
var friendsDescriptor = Object.getOwnPropertyDescriptor(obj, 'friends');
expect(friendsDescriptor.get).to.equal(undefined);
expect(friendsDescriptor.set).to.equal(undefined);
 
ko.isObservable(obj.friends).should.be.true;
});
 
it('should deeply create observableModels from arrays of objects', function() {
var obj = ko.observableModel({
name: 'Bob',
friends: [{
name: 'Jill'
}, {
name: ko.computed(function() {
return 'Jane';
})
}]
});
 
ko.isObservable(obj._friends).should.be.true;
obj._friends.push.should.be.a('function');
obj.friends[0].name.should.equal('Jill');
ko.isObservable(obj.friends[0]._name).should.be.true;
ko.isObservable(obj.friends[1]._name).should.be.false;
obj.friends[1].name().should.equal('Jane');
});
});
 
describe('observableArrayModel', function() {
it('should return a ko.observableArray', function() {
var stuff = ko.observableArrayModel(['hi', 'there']);
ko.isObservable(stuff).should.be.true;
stuff().should.be.an('array');
});
 
it('should turn object members into observableModels, deeply by default', function() {
var stuff = ko.observableArrayModel(['hi', { world: 'there' }]);
 
ko.isObservable(stuff()[0]).should.be.false;
stuff()[0].should.equal('hi');
 
ko.isObservable(stuff()[1]._world).should.be.true;
stuff()[1].world.should.equal('there');
});
});
});
</script>
 
<script type="text/javascript">
(function() {
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
})();
</script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.