Skip to content

Commit 0926751

Browse files
committed
Basic test suite and a bunch of fixes
Fixes #45 (compatibility with KO 3.4.2), Fixes #46 (forget to unwatch objects), Fixes #47 (bad parents in notification after object swap), Fixes #48 (basic test suite). Updated dev dependencies, add license/version banner to dist files.
1 parent ad55021 commit 0926751

File tree

8 files changed

+291
-25
lines changed

8 files changed

+291
-25
lines changed

Gruntfile.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function(grunt) {
88
var banner = [
99
"<%= pkg.name %> v<%= pkg.version %>",
1010
"The MIT License (MIT)",
11-
"Copyright (c) 2016 <%= pkg.author %>"
11+
"Copyright (c) 2017 <%= pkg.author %>",
1212
].join("\n * ").trim();
1313

1414
grunt.initConfig({
@@ -17,7 +17,7 @@ module.exports = function(grunt) {
1717

1818
concat: {
1919
options: {
20-
//banner: "/*! " + banner + " */\n\n"
20+
banner: "/*! " + banner + " */\n"
2121
},
2222
copy: {
2323
files: {
@@ -32,9 +32,10 @@ module.exports = function(grunt) {
3232

3333
uglify: {
3434
options: {
35-
//banner: "/*! " + banner + " */\n",
3635
footer: "window.foo = \"<%= pkg.version %>\";",
37-
preserveComments: 'some'
36+
output: {
37+
comments: '/^!/'
38+
}
3839
},
3940
main: {
4041
files: {
@@ -48,13 +49,26 @@ module.exports = function(grunt) {
4849
files: 'src/*.js',
4950
tasks: ['jshint', 'uglify']
5051
}
52+
},
53+
54+
jasmine: {
55+
main: {
56+
src: 'src/**/*.js',
57+
options: {
58+
specs: 'spec/*.js',
59+
vendor: [
60+
'https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js'
61+
]
62+
}
63+
}
5164
}
5265
});
5366

5467
grunt.loadNpmTasks('grunt-contrib-concat');
5568
grunt.loadNpmTasks('grunt-contrib-jshint');
5669
grunt.loadNpmTasks('grunt-contrib-uglify');
5770
grunt.loadNpmTasks('grunt-contrib-watch');
71+
grunt.loadNpmTasks('grunt-contrib-jasmine');
5872

5973
grunt.registerTask('default', ['concat', 'uglify']);
6074
grunt.registerTask('develop', ['concat', 'uglify', 'watch']);

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ Pausing and resuming a reactor on any property can be done like so:
133133
//...do work
134134
this.data.watch(true);
135135
136+
<b>Dispose/Unwatch:</b><br/>
137+
The ```watch``` function returns an object with a "dispose" method you can call to dispose the watch action:
138+
139+
var viewModel = { ... };
140+
var res = ko.watch(viewModel, { depth: -1 }, function(parents, child, item) {
141+
...
142+
});
143+
144+
res.dispose();
145+
146+
Once disposed your model will be "unwatched"<br/>
147+
136148
<b>Projects using KO-Reactor:</b><br/>
137149
As of now I'm only aware of this excellent undo manager by Stefano Bagnara:
138150
@@ -159,3 +171,22 @@ Yet another usage not mentioned above example involves calling ```watch``` on a
159171
However it is nothing more than a cosmetic enhancement since it only returns a ```computed``` of the function being passed in.
160172
161173
For an extensive list of available options please consult the ```param``` sections of the source code.
174+
175+
Development
176+
===========
177+
178+
In order to build and minify you can simply call grunt:
179+
180+
grunt
181+
182+
If you want a live rebuild for each changed
183+
184+
grunt develop
185+
186+
If you want to run the test suite:
187+
188+
grunt jasmine
189+
190+
If you want to generate the test runner in order to run the test suite in your browser
191+
192+
grunt jasmine:main:build

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"homepage": "https://github.com/ZiadJ/knockoutjs-reactor.git",
44
"authors": [
55
"Ziad Jeeroburkhan",
6-
"Jacob Kelley <jakie8@gmail.com>"
6+
"Jacob Kelley <jakie8@gmail.com>",
7+
"Stefano Bagnara <github@bago.org>"
78
],
89
"description": "Deeply watches observable changes",
910
"main": [

dist/ko-reactor.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/*! ko-reactor v1.4.0-beta
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017 Ziad Jeeroburkhan */
14
// Deep observer plugin for Knockout http://knockoutjs.com/
25
// (c) Ziad Jeeroburkhan
36
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
@@ -209,7 +212,7 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
209212
case "3.3.0": subscriptionsField = 'G'; break;
210213
case "3.4.0": subscriptionsField = 'K'; break;
211214
case "3.4.1": subscriptionsField = 'K'; break;
212-
case "3.4.2": subscriptionsField = 'K'; break;
215+
case "3.4.2": subscriptionsField = 'F'; break;
213216
default: throw "Unsupported Knockout version. Only v3.0.0 to v3.4.2 are supported when minified. Current version is " + ko.version;
214217
}
215218

@@ -248,9 +251,8 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
248251

249252
if (!item.moved) {
250253
// Deleted or brand new item. Unwatch or watch it accordingly.
251-
setTimeout(function () {
252-
watchChildren(item.value, (keepOffParentList ? null : child), parents, item.status === 'deleted');
253-
}, 0);
254+
// This used to be on a setTimeout but this is not symmetric to the !array case.
255+
watchChildren(item.value, (keepOffParentList ? null : child), parents, item.status === 'deleted');
254256
}
255257
});
256258
}, undefined, 'arrayChange')._watcher = context;
@@ -265,7 +267,7 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
265267

266268
if (options.mutable && typeof child() === 'object')
267269
// Watch the new comer.
268-
watchChildren(child(), (keepOffParentList ? null : child), parents);
270+
watchChildren(child(), (keepOffParentList ? null : child), parents, false, true);
269271
}
270272

271273
}, null, 'change')._watcher = context;
@@ -286,7 +288,7 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
286288

287289
if (options.mutable && typeof oldValue === 'object')
288290
// Clean up all subscriptions for the old child object.
289-
watchChildren(oldValue, (keepOffParentList ? null : child), parents, false, true);
291+
watchChildren(oldValue, (keepOffParentList ? null : child), parents, true, true);
290292

291293
}, null, 'beforeChange')._watcher = context;
292294
}

dist/ko-reactor.min.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ko-reactor",
33
"author": "Ziad Jeeroburkhan",
4-
"version": "1.3.9",
4+
"version": "1.4.0-beta",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/ZiadJ/knockoutjs-reactor.git"
@@ -13,11 +13,12 @@
1313
},
1414
"devDependencies": {
1515
"bower": "*",
16-
"grunt": "~0.4.1",
17-
"grunt-cli": "~0.1.9",
18-
"grunt-contrib-concat": "^0.5.0",
19-
"grunt-contrib-jshint": "0.8.0",
20-
"grunt-contrib-uglify": "0.2.5",
21-
"grunt-contrib-watch": "~0.5.3"
16+
"grunt": "~1.0.1",
17+
"grunt-cli": "~1.2.0",
18+
"grunt-contrib-concat": "~1.0.1",
19+
"grunt-contrib-jasmine": "~1.1.0",
20+
"grunt-contrib-jshint": "~1.1.0",
21+
"grunt-contrib-uglify": "~3.1.0",
22+
"grunt-contrib-watch": "~1.0.0"
2223
}
2324
}

0 commit comments

Comments
 (0)