@@ -108,6 +108,8 @@ var _default = /*#__PURE__*/function (_Controller) {
108
108
} , {
109
109
key : "connect" ,
110
110
value : function connect ( ) {
111
+ var _this2 = this ;
112
+
111
113
// hide "loading" elements to begin with
112
114
// This is done with CSS, but only for the most basic cases
113
115
this . _onLoadingFinish ( ) ;
@@ -117,6 +119,14 @@ var _default = /*#__PURE__*/function (_Controller) {
117
119
}
118
120
119
121
window . addEventListener ( 'beforeunload' , this . markAsWindowUnloaded ) ;
122
+ this . element . addEventListener ( 'live:update-model' , function ( event ) {
123
+ // ignore events that we dispatched
124
+ if ( event . target === _this2 . element ) {
125
+ return ;
126
+ }
127
+
128
+ _this2 . _handleChildComponentUpdateModel ( event ) ;
129
+ } ) ;
120
130
121
131
this . _dispatchEvent ( 'live:connect' ) ;
122
132
}
@@ -149,7 +159,7 @@ var _default = /*#__PURE__*/function (_Controller) {
149
159
} , {
150
160
key : "action" ,
151
161
value : function action ( event ) {
152
- var _this2 = this ;
162
+ var _this3 = this ;
153
163
154
164
// using currentTarget means that the data-action and data-action-name
155
165
// must live on the same element: you can't add
@@ -169,9 +179,9 @@ var _default = /*#__PURE__*/function (_Controller) {
169
179
// then the action Ajax will start. We want to avoid the
170
180
// re-render request from starting after the debounce and
171
181
// taking precedence
172
- _this2 . _clearWaitingDebouncedRenders ( ) ;
182
+ _this3 . _clearWaitingDebouncedRenders ( ) ;
173
183
174
- _this2 . _makeRequest ( directive . action ) ;
184
+ _this3 . _makeRequest ( directive . action ) ;
175
185
} ;
176
186
177
187
var handled = false ;
@@ -196,13 +206,13 @@ var _default = /*#__PURE__*/function (_Controller) {
196
206
{
197
207
var length = modifier . value ? modifier . value : DEFAULT_DEBOUNCE ; // clear any pending renders
198
208
199
- if ( _this2 . actionDebounceTimeout ) {
200
- clearTimeout ( _this2 . actionDebounceTimeout ) ;
201
- _this2 . actionDebounceTimeout = null ;
209
+ if ( _this3 . actionDebounceTimeout ) {
210
+ clearTimeout ( _this3 . actionDebounceTimeout ) ;
211
+ _this3 . actionDebounceTimeout = null ;
202
212
}
203
213
204
- _this2 . actionDebounceTimeout = setTimeout ( function ( ) {
205
- _this2 . actionDebounceTimeout = null ;
214
+ _this3 . actionDebounceTimeout = setTimeout ( function ( ) {
215
+ _this3 . actionDebounceTimeout = null ;
206
216
207
217
_executeAction ( ) ;
208
218
} , length ) ;
@@ -236,14 +246,14 @@ var _default = /*#__PURE__*/function (_Controller) {
236
246
throw new Error ( "The update() method could not be called for \"" . concat ( clonedElement . outerHTML , "\": the element must either have a \"data-model\" or \"name\" attribute set to the model name." ) ) ;
237
247
}
238
248
239
- this . $updateModel ( model , value , element , shouldRender ) ;
249
+ this . $updateModel ( model , value , shouldRender ) ;
240
250
}
241
251
} , {
242
252
key : "$updateModel" ,
243
- value : function $updateModel ( model , value , element ) {
244
- var _this3 = this ;
253
+ value : function $updateModel ( model , value ) {
254
+ var _this4 = this ;
245
255
246
- var shouldRender = arguments . length > 3 && arguments [ 3 ] !== undefined ? arguments [ 3 ] : true ;
256
+ var shouldRender = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : true ;
247
257
var directives = ( 0 , _directives_parser . parseDirectives ) ( model ) ;
248
258
249
259
if ( directives . length > 1 ) {
@@ -272,7 +282,12 @@ var _default = /*#__PURE__*/function (_Controller) {
272
282
273
283
if ( ! ( 0 , _set_deep_data . doesDeepPropertyExist ) ( this . dataValue , modelName ) ) {
274
284
console . warn ( "Model \"" . concat ( modelName , "\" is not a valid data-model value" ) ) ;
275
- } // we do not send old and new data to the server
285
+ }
286
+
287
+ this . _dispatchEvent ( 'live:update-model' , {
288
+ model : modelName ,
289
+ value : value
290
+ } ) ; // we do not send old and new data to the server
276
291
// we merge in the new data now
277
292
// TODO: handle edge case for top-level of a model with "exposed" props
278
293
// For example, suppose there is a "post" field but "post.title" is exposed.
@@ -301,16 +316,16 @@ var _default = /*#__PURE__*/function (_Controller) {
301
316
this . _clearWaitingDebouncedRenders ( ) ;
302
317
303
318
this . renderDebounceTimeout = setTimeout ( function ( ) {
304
- _this3 . renderDebounceTimeout = null ;
319
+ _this4 . renderDebounceTimeout = null ;
305
320
306
- _this3 . $render ( ) ;
321
+ _this4 . $render ( ) ;
307
322
} , this . debounceValue || DEFAULT_DEBOUNCE ) ;
308
323
}
309
324
}
310
325
} , {
311
326
key : "_makeRequest" ,
312
327
value : function _makeRequest ( action ) {
313
- var _this4 = this ;
328
+ var _this5 = this ;
314
329
315
330
var _this$urlValue$split = this . urlValue . split ( '?' ) ,
316
331
_this$urlValue$split2 = _slicedToArray ( _this$urlValue$split , 2 ) ,
@@ -347,15 +362,15 @@ var _default = /*#__PURE__*/function (_Controller) {
347
362
this . renderPromiseStack . addPromise ( thisPromise ) ;
348
363
thisPromise . then ( function ( response ) {
349
364
// if another re-render is scheduled, do not "run it over"
350
- if ( _this4 . renderDebounceTimeout ) {
365
+ if ( _this5 . renderDebounceTimeout ) {
351
366
return ;
352
367
}
353
368
354
- var isMostRecent = _this4 . renderPromiseStack . removePromise ( thisPromise ) ;
369
+ var isMostRecent = _this5 . renderPromiseStack . removePromise ( thisPromise ) ;
355
370
356
371
if ( isMostRecent ) {
357
372
response . json ( ) . then ( function ( data ) {
358
- _this4 . _processRerender ( data ) ;
373
+ _this5 . _processRerender ( data ) ;
359
374
} ) ;
360
375
}
361
376
} ) ;
@@ -424,21 +439,21 @@ var _default = /*#__PURE__*/function (_Controller) {
424
439
} , {
425
440
key : "_handleLoadingToggle" ,
426
441
value : function _handleLoadingToggle ( isLoading ) {
427
- var _this5 = this ;
442
+ var _this6 = this ;
428
443
429
444
this . _getLoadingDirectives ( ) . forEach ( function ( _ref ) {
430
445
var element = _ref . element ,
431
446
directives = _ref . directives ;
432
447
433
448
// so we can track, at any point, if an element is in a "loading" state
434
449
if ( isLoading ) {
435
- _this5 . _addAttributes ( element , [ 'data-live-is-loading' ] ) ;
450
+ _this6 . _addAttributes ( element , [ 'data-live-is-loading' ] ) ;
436
451
} else {
437
- _this5 . _removeAttributes ( element , [ 'data-live-is-loading' ] ) ;
452
+ _this6 . _removeAttributes ( element , [ 'data-live-is-loading' ] ) ;
438
453
}
439
454
440
455
directives . forEach ( function ( directive ) {
441
- _this5 . _handleLoadingDirective ( element , isLoading , directive ) ;
456
+ _this6 . _handleLoadingDirective ( element , isLoading , directive ) ;
442
457
} ) ;
443
458
} ) ;
444
459
}
@@ -452,50 +467,50 @@ var _default = /*#__PURE__*/function (_Controller) {
452
467
} , {
453
468
key : "_handleLoadingDirective" ,
454
469
value : function _handleLoadingDirective ( element , isLoading , directive ) {
455
- var _this6 = this ;
470
+ var _this7 = this ;
456
471
457
472
var finalAction = parseLoadingAction ( directive . action , isLoading ) ;
458
473
var loadingDirective = null ;
459
474
460
475
switch ( finalAction ) {
461
476
case 'show' :
462
477
loadingDirective = function loadingDirective ( ) {
463
- _this6 . _showElement ( element ) ;
478
+ _this7 . _showElement ( element ) ;
464
479
} ;
465
480
466
481
break ;
467
482
468
483
case 'hide' :
469
484
loadingDirective = function loadingDirective ( ) {
470
- return _this6 . _hideElement ( element ) ;
485
+ return _this7 . _hideElement ( element ) ;
471
486
} ;
472
487
473
488
break ;
474
489
475
490
case 'addClass' :
476
491
loadingDirective = function loadingDirective ( ) {
477
- return _this6 . _addClass ( element , directive . args ) ;
492
+ return _this7 . _addClass ( element , directive . args ) ;
478
493
} ;
479
494
480
495
break ;
481
496
482
497
case 'removeClass' :
483
498
loadingDirective = function loadingDirective ( ) {
484
- return _this6 . _removeClass ( element , directive . args ) ;
499
+ return _this7 . _removeClass ( element , directive . args ) ;
485
500
} ;
486
501
487
502
break ;
488
503
489
504
case 'addAttribute' :
490
505
loadingDirective = function loadingDirective ( ) {
491
- return _this6 . _addAttributes ( element , directive . args ) ;
506
+ return _this7 . _addAttributes ( element , directive . args ) ;
492
507
} ;
493
508
494
509
break ;
495
510
496
511
case 'removeAttribute' :
497
512
loadingDirective = function loadingDirective ( ) {
498
- return _this6 . _removeAttributes ( element , directive . args ) ;
513
+ return _this7 . _removeAttributes ( element , directive . args ) ;
499
514
} ;
500
515
501
516
break ;
@@ -599,7 +614,7 @@ var _default = /*#__PURE__*/function (_Controller) {
599
614
} , {
600
615
key : "_executeMorphdom" ,
601
616
value : function _executeMorphdom ( newHtml ) {
602
- var _this7 = this ;
617
+ var _this8 = this ;
603
618
604
619
// https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro#answer-35385518
605
620
function htmlToElement ( html ) {
@@ -618,7 +633,7 @@ var _default = /*#__PURE__*/function (_Controller) {
618
633
} // avoid updating child components: they will handle themselves
619
634
620
635
621
- if ( fromEl . hasAttribute ( 'data-controller' ) && fromEl . getAttribute ( 'data-controller' ) . split ( ' ' ) . indexOf ( 'live' ) !== - 1 && fromEl !== _this7 . element ) {
636
+ if ( fromEl . hasAttribute ( 'data-controller' ) && fromEl . getAttribute ( 'data-controller' ) . split ( ' ' ) . indexOf ( 'live' ) !== - 1 && fromEl !== _this8 . element ) {
622
637
return false ;
623
638
}
624
639
@@ -629,7 +644,7 @@ var _default = /*#__PURE__*/function (_Controller) {
629
644
} , {
630
645
key : "_initiatePolling" ,
631
646
value : function _initiatePolling ( rawPollConfig ) {
632
- var _this8 = this ;
647
+ var _this9 = this ;
633
648
634
649
var directives = ( 0 , _directives_parser . parseDirectives ) ( rawPollConfig || '$render' ) ;
635
650
directives . forEach ( function ( directive ) {
@@ -648,23 +663,23 @@ var _default = /*#__PURE__*/function (_Controller) {
648
663
}
649
664
} ) ;
650
665
651
- _this8 . startPoll ( directive . action , duration ) ;
666
+ _this9 . _startPoll ( directive . action , duration ) ;
652
667
} ) ;
653
668
}
654
669
} , {
655
- key : "startPoll " ,
656
- value : function startPoll ( actionName , duration ) {
657
- var _this9 = this ;
670
+ key : "_startPoll " ,
671
+ value : function _startPoll ( actionName , duration ) {
672
+ var _this10 = this ;
658
673
659
674
var callback ;
660
675
661
676
if ( actionName . charAt ( 0 ) === '$' ) {
662
677
callback = function callback ( ) {
663
- _this9 [ actionName ] ( ) ;
678
+ _this10 [ actionName ] ( ) ;
664
679
} ;
665
680
} else {
666
681
callback = function callback ( ) {
667
- _this9 . _makeRequest ( actionName ) ;
682
+ _this10 . _makeRequest ( actionName ) ;
668
683
} ;
669
684
}
670
685
@@ -685,6 +700,15 @@ var _default = /*#__PURE__*/function (_Controller) {
685
700
} ) ;
686
701
return this . element . dispatchEvent ( userEvent ) ;
687
702
}
703
+ } , {
704
+ key : "_handleChildComponentUpdateModel" ,
705
+ value : function _handleChildComponentUpdateModel ( event ) {
706
+ if ( ! ( 0 , _set_deep_data . doesDeepPropertyExist ) ( this . dataValue , event . detail . model ) ) {
707
+ return ;
708
+ }
709
+
710
+ this . $updateModel ( event . detail . model , event . detail . value , false ) ;
711
+ }
688
712
} ] ) ;
689
713
690
714
return _default ;
0 commit comments