1
- /*! ko-reactor v1.4.0-beta
1
+ /*! ko-reactor v1.4.0-beta2
2
2
* The MIT License (MIT)
3
3
* Copyright (c) 2017 Ziad Jeeroburkhan */
4
4
// Deep observer plugin for Knockout http://knockoutjs.com/
@@ -39,6 +39,8 @@ ko.subscribable.fn['watch'] = function (targetOrCallback, options, evaluatorCall
39
39
/// { tagFields: true } -> Add the property '_fieldName' under each property for textual identification.<br/>
40
40
/// { tagFields: 'parentsOnly' } -> Same as above except that it is limited to parent properties only.<br/>
41
41
/// { oldValues: 3 } -> Keep the last three values for each subscribable under the property 'oldValues'.<br/>
42
+ /// { synchWatch: true } -> Use setTimeout to start watching new objects
43
+ /// { splitArrayChanges: false } -> receive a single notification for array changes as an array of "items" instead of multiple notifications
42
44
/// { seal: true } -> Prevent any subsequent watcher from watching the target again.<br/>
43
45
/// { unloop: true } -> Avoid circular paths through the use of a breadcrumb property '_watcher' set at each node level.<br/>
44
46
/// </param>
@@ -244,15 +246,28 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
244
246
if ( isArray ) {
245
247
// Child is an observable array. Watch all changes within it.
246
248
child . subscribe ( function ( changes ) {
247
- ko . utils . arrayForEach ( changes , function ( item ) {
248
- var returnValue = evaluatorCallback . call ( context , parents , child , item ) ;
249
+ var returnValue ;
250
+ if ( options . splitArrayChanges === false ) {
251
+ returnValue = evaluatorCallback . call ( context , parents , child , changes ) ;
249
252
if ( returnValue !== undefined )
250
253
context ( returnValue ) ;
254
+ }
255
+ ko . utils . arrayForEach ( changes , function ( item ) {
256
+ if ( options . splitArrayChanges !== false ) {
257
+ var returnValue = evaluatorCallback . call ( context , parents , child , item ) ;
258
+ if ( returnValue !== undefined )
259
+ context ( returnValue ) ;
260
+ }
251
261
252
262
if ( ! item . moved ) {
253
263
// Deleted or brand new item. Unwatch or watch it accordingly.
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' ) ;
264
+ if ( options . synchWatch ) {
265
+ watchChildren ( item . value , ( keepOffParentList ? null : child ) , parents , item . status === 'deleted' ) ;
266
+ } else {
267
+ setTimeout ( function ( ) {
268
+ watchChildren ( item . value , ( keepOffParentList ? null : child ) , parents , item . status === 'deleted' ) ;
269
+ } ) ;
270
+ }
256
271
}
257
272
} ) ;
258
273
} , undefined , 'arrayChange' ) . _watcher = context ;
@@ -265,9 +280,16 @@ ko['watch'] = function (target, options, evaluatorCallback, context) {
265
280
if ( returnValue !== undefined )
266
281
context ( returnValue ) ;
267
282
268
- if ( options . mutable && typeof child ( ) === 'object' )
283
+ if ( options . mutable && typeof child ( ) === 'object' ) {
269
284
// Watch the new comer.
270
- watchChildren ( child ( ) , ( keepOffParentList ? null : child ) , parents , false , true ) ;
285
+ if ( options . synchWatch ) {
286
+ watchChildren ( child ( ) , ( keepOffParentList ? null : child ) , parents , false , true ) ;
287
+ } else {
288
+ setTimeout ( function ( ) {
289
+ watchChildren ( child ( ) , ( keepOffParentList ? null : child ) , parents , false , true ) ;
290
+ } ) ;
291
+ }
292
+ }
271
293
}
272
294
273
295
} , null , 'change' ) . _watcher = context ;
0 commit comments