1
- /*! jquery.views.js v0.9.86 (Beta): http://jsviews.com/ */
1
+ /*! jquery.views.js v0.9.87 (Beta): http://jsviews.com/ */
2
2
/*
3
3
* Interactive data-driven views using JsRender templates.
4
4
* Subcomponent of JsViews
@@ -44,7 +44,7 @@ var setGlobals = $ === false; // Only set globals if script block in browser (no
44
44
jsr = jsr || setGlobals && global . jsrender ;
45
45
$ = $ || global . jQuery ;
46
46
47
- var versionNumber = "v0.9.86 " ,
47
+ var versionNumber = "v0.9.87 " ,
48
48
requiresStr = "JsViews requires " ;
49
49
50
50
if ( ! $ || ! $ . fn ) {
@@ -158,8 +158,8 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
158
158
// Observably update data values targeted by bindTo
159
159
// Called when linkedElem changes: called as updateValues(sourceValues, tagElse, bindId, ev) - this: undefined
160
160
// Called directly as tag.updateValues(val1, val2, val3, ...) - this: tag
161
- var cancel , linkCtx , cvtBack , cnvtName , target , view , binding , sourceValue , origVals , sourceElem , sourceEl ,
162
- oldLinkCtx , onBeforeChange , onAfterChange , tos , to , tcpTag , eventArgs , exprOb , contextCb , l , srcVals , tag ;
161
+ var linkCtx , cvtBack , cnvtName , target , view , binding , sourceValue , origVals , sourceElem , sourceEl ,
162
+ oldLinkCtx , tos , to , tcpTag , exprOb , contextCb , l , tag ;
163
163
164
164
if ( bindId && bindId . _tgId ) {
165
165
tag = bindId ;
@@ -172,6 +172,7 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
172
172
// The binding has a 'to' field, which is of the form [tosForElse0, tosForElse1, ...]
173
173
// where tosForElseX is of the form [[[targetObject, toPath], [targetObject, toPath], ...], cvtBack]
174
174
linkCtx = binding . linkCtx ;
175
+ linkCtx . ev = ev ;
175
176
sourceElem = linkCtx . elem ;
176
177
view = linkCtx . view ;
177
178
tag = linkCtx . tag ;
@@ -181,8 +182,6 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
181
182
sourceValues = [ ] ;
182
183
sourceValues [ tos . _cxp . ind ] = sourceValue ;
183
184
}
184
- onBeforeChange = changeHandler ( view , onBeforeChangeStr ) ;
185
- onAfterChange = changeHandler ( view , onAfterChangeStr ) ;
186
185
187
186
if ( cnvtName = tag && tag . convertBack ) {
188
187
if ( $isFunction ( cnvtName ) ) {
@@ -202,7 +201,7 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
202
201
origVals = sourceValues ;
203
202
if ( cvtBack ) {
204
203
sourceValues = cvtBack . apply ( tag , sourceValues ) ;
205
- sourceValues = tos . length > 1 ? sourceValues : [ sourceValues ] ;
204
+ sourceValues = tos . length > 1 ? sourceValues || [ ] : [ sourceValues ] ;
206
205
}
207
206
208
207
// Set linkCtx on view, dynamically, just during this handler call
@@ -220,17 +219,16 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
220
219
// binding to tag.ctx.foo._ocp - and we use original values, without applying cvtBack converter
221
220
: sourceValues // Otherwise use the converted value
222
221
) [ l ] ;
223
- eventArgs = {
224
- change : "change" ,
225
- data : tcpTag || target , // For tag contextual parameter, this is the tag
226
- path : to [ 1 ] || to . ind , // For tag contextual parameter, this is the index of the parameter in tag.bindTo
227
- value : sourceValue
228
- } ;
229
- if ( ( ! onBeforeChange || ! ( cancel = onBeforeChange . call ( linkCtx , ev , eventArgs ) === false ) ) &&
230
- ( ! tag || ! tag . onBeforeChange || ! ( cancel = tag . onBeforeChange ( ev , eventArgs ) === false ) ) ) {
231
-
232
- if ( tcpTag ) {
233
- tcpTag . updateValue ( sourceValue , to . ind , to . tagElse ) ;
222
+ if ( sourceValue !== undefined && ( ! ev || ! tag || ! tag . onBeforeUpdateVal || tag . onBeforeUpdateVal ( ev , {
223
+ change : "change" ,
224
+ data : target ,
225
+ path : to [ 1 ] ,
226
+ index : l ,
227
+ tagElse : tagElse ,
228
+ value : sourceValue
229
+ } ) !== false ) ) {
230
+ if ( tcpTag ) { // We are modifying a tag contextual parameter ~foo (e.g. from within block) so update 'owner' tag: tcpTag
231
+ tcpTag . updateValue ( sourceValue , to . ind , to . tagElse , undefined , ev ) ;
234
232
if ( tcpTag . setValue ) {
235
233
tcpTag . setValue ( sourceValue , to . ind , to . tagElse ) ;
236
234
}
@@ -253,17 +251,11 @@ function updateValues(sourceValues, tagElse, bindId, ev) {
253
251
}
254
252
}
255
253
$observable ( target ) . setProperty ( to [ 1 ] , sourceValue ) ; // 2way binding change event - observably updating bound object
256
- if ( onAfterChange ) {
257
- onAfterChange . call ( linkCtx , ev , eventArgs ) ;
258
- }
259
- }
260
- if ( tag && tag . onAfterChange ) {
261
- tag . onAfterChange ( ev , eventArgs ) ;
262
254
}
263
255
}
264
256
}
265
257
}
266
- sourceElem . _jsvChg = undefined ; // Clear marker
258
+ sourceElem . _jsvChg = linkCtx . ev = undefined ; // Clear marker
267
259
view . linkCtx = oldLinkCtx ;
268
260
}
269
261
}
@@ -353,11 +345,11 @@ function onDataLinkedTagChange(ev, eventArgs) {
353
345
parentElem = target . parentNode ,
354
346
view = linkCtx . view ,
355
347
oldLinkCtx = view . linkCtx ,
356
- onEvent = changeHandler ( view , onBeforeChangeStr ) ;
348
+ onEvent = eventArgs && changeHandler ( view , onBeforeChangeStr , tag ) ;
357
349
358
350
// Set linkCtx on view, dynamically, just during this handler call
359
351
view . linkCtx = linkCtx ;
360
- if ( parentElem && ( ! onEvent || ! ( eventArgs && onEvent . call ( linkCtx , ev , eventArgs ) === false ) )
352
+ if ( parentElem && ( ! onEvent || onEvent . call ( linkCtx , ev , eventArgs ) !== false )
361
353
// If data changed, the ev.data is set to be the path. Use that to filter the handler action...
362
354
&& ( ! eventArgs || ev . data . prop === "*" || ev . data . prop === eventArgs . path ) ) {
363
355
@@ -409,6 +401,9 @@ function onDataLinkedTagChange(ev, eventArgs) {
409
401
observeAndBind ( linkCtx , source , target ) ;
410
402
}
411
403
view . linkCtx = oldLinkCtx ;
404
+ if ( eventArgs && ( onEvent = changeHandler ( view , onAfterChangeStr , tag ) ) ) {
405
+ onEvent . call ( linkCtx , ev , eventArgs ) ;
406
+ }
412
407
return ;
413
408
}
414
409
@@ -436,9 +431,7 @@ function onDataLinkedTagChange(ev, eventArgs) {
436
431
// will trigger the parent tags before the child tags.
437
432
observeAndBind ( linkCtx , source , target ) ;
438
433
}
439
- if ( updateContent ( sourceValue , linkCtx , attr , tag ) && eventArgs && ( onEvent = changeHandler ( view , onAfterChangeStr ) ) ) {
440
- onEvent . call ( linkCtx , ev , eventArgs ) ;
441
- }
434
+ updateContent ( sourceValue , linkCtx , attr , tag ) ;
442
435
linkCtx . _noUpd = 0 ; // For data-link="^{...}" remove _noUpd flag so updates on subsequent calls
443
436
444
437
if ( tag ) {
@@ -453,6 +446,9 @@ function onDataLinkedTagChange(ev, eventArgs) {
453
446
observeAndBind ( linkCtx , source , target ) ;
454
447
}
455
448
449
+ if ( eventArgs && ( onEvent = changeHandler ( view , onAfterChangeStr , tag ) ) ) {
450
+ onEvent . call ( linkCtx , ev , eventArgs ) ;
451
+ }
456
452
// Remove dynamically added linkCtx from view
457
453
view . linkCtx = oldLinkCtx ;
458
454
}
@@ -639,8 +635,8 @@ function updateContent(sourceValue, linkCtx, attr, tag) {
639
635
640
636
function arrayChangeHandler ( ev , eventArgs ) {
641
637
var self = this ,
642
- onBeforeChange = changeHandler ( self , onBeforeChangeStr ) ,
643
- onAfterChange = changeHandler ( self , onAfterChangeStr ) ;
638
+ onBeforeChange = changeHandler ( self , onBeforeChangeStr , self . tag ) ,
639
+ onAfterChange = changeHandler ( self , onAfterChangeStr , self . tag ) ;
644
640
if ( ! onBeforeChange || onBeforeChange . call ( self , ev , eventArgs ) !== false ) {
645
641
if ( eventArgs ) {
646
642
// This is an observable action (not a trigger/handler call from pushValues, or similar, for which eventArgs will be null)
@@ -2089,9 +2085,7 @@ function resolveDataTargetPath(targetPath, source, contextCb) {
2089
2085
targetPath = to [ 1 ] ;
2090
2086
}
2091
2087
to = to || [ source , path ] ;
2092
- if ( obsCtxPrm ) {
2093
- to . _cxp = obsCtxPrm ;
2094
- }
2088
+ to . _cxp = topCp ;
2095
2089
return to ;
2096
2090
}
2097
2091
@@ -2270,9 +2264,9 @@ function inputAttrib(elem) {
2270
2264
return elem . type === CHECKBOX ? elem [ CHECKED ] : elem . value ;
2271
2265
}
2272
2266
2273
- function changeHandler ( view , name ) {
2267
+ function changeHandler ( view , name , tag ) {
2274
2268
// Get onBeforeChange, onAfterChange, onAfterCreate handler - if there is one;
2275
- return view . ctx [ name ] || $views . helpers [ name ] ;
2269
+ return tag && tag [ name ] || view . ctx [ name ] && view . ctxPrm ( name ) || $views . helpers [ name ] ;
2276
2270
}
2277
2271
2278
2272
//========================== Initialize ==========================
@@ -3447,7 +3441,7 @@ $sub._gccb = function(view) { // Return a callback for accessing the context of
3447
3441
return function ( path , object , depth ) {
3448
3442
// TODO consider only calling the contextCb on the initial token in path '~a.b.c' and not calling again on
3449
3443
// the individual tokens, 'a', 'b', 'c'... Currently it is called multiple times
3450
- var tokens , tag , items , helper , last , nextPath , l , obsCtxPrm , addedTagCpDep , key ;
3444
+ var tokens , tag , items , helper , last , nextPath , l , obsCtxPrm , addedTagCpDep , key , bindTo ;
3451
3445
if ( view && path ) {
3452
3446
if ( path . _cpfn ) {
3453
3447
return path . _cpfn . call ( view . tmpl , object , view , $sub ) ; // exprOb for computed property
@@ -3486,10 +3480,11 @@ $sub._gccb = function(view) { // Return a callback for accessing the context of
3486
3480
items = [ helper ] ; // Contextual parameter
3487
3481
if ( ( tag = obsCtxPrm . tag ) && tag . convert ) {
3488
3482
// If there is a converter, it might mix inputs, so tag contextual param needs to depends on all bound args/props.
3489
- l = tag . bindTo . length ;
3483
+ bindTo = tag . bindTo || [ 0 ] ;
3484
+ l = bindTo . length ;
3490
3485
while ( l -- ) {
3491
3486
if ( depth !== undefined && l !== obsCtxPrm . ind ) {
3492
- key = tag . bindTo [ l ] ;
3487
+ key = bindTo [ l ] ;
3493
3488
addedTagCpDep = [ helper [ 0 ] , tag . tagCtx . params [ + key === key ? "args" : "props" ] ] ;
3494
3489
addedTagCpDep . _cxp = obsCtxPrm ;
3495
3490
items . push ( addedTagCpDep ) ; // Added dependency for tag contextual parameter
0 commit comments