@@ -151,7 +151,30 @@ export class _MatTableDataSource<
151151 * @param data Data object that is being accessed.
152152 * @param sortHeaderId The name of the column that represents the data.
153153 */
154- sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number = (
154+ sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number ;
155+
156+ /**
157+ * `sortingDataAccessor` is currently a regular property, however we need to know when it is
158+ * updated so that we can re-render the data. The problem is that we can't turn it into a
159+ * getter/setter, because it'll break existing users that define the accessor by extending the
160+ * class and initializing it to a new value. This is a workaround that'll preserve the old
161+ * behavior for users who override it and apply the fix for ones who don't. Eventually we should
162+ * reconcile these behaviors.
163+ * @breaking -change 15.0.0
164+ */
165+ private _defineSortingDataAccessor ( ) {
166+ if ( ! this . sortingDataAccessor ) {
167+ Object . defineProperty ( this , 'sortingDataAccessor' , {
168+ get : ( ) => this . _sortingDataAccessor ,
169+ set : accessor => {
170+ this . _sortingDataAccessor = accessor ;
171+ this . _updateChangeSubscription ( ) ;
172+ } ,
173+ } ) ;
174+ }
175+ }
176+
177+ private _sortingDataAccessor : ( data : T , sortHeaderId : string ) => string | number = (
155178 data : T ,
156179 sortHeaderId : string ,
157180 ) : string | number => {
@@ -258,7 +281,6 @@ export class _MatTableDataSource<
258281 constructor ( initialData : T [ ] = [ ] ) {
259282 super ( ) ;
260283 this . _data = new BehaviorSubject < T [ ] > ( initialData ) ;
261- this . _updateChangeSubscription ( ) ;
262284 }
263285
264286 /**
@@ -267,6 +289,8 @@ export class _MatTableDataSource<
267289 * the provided base data and send it to the table for rendering.
268290 */
269291 _updateChangeSubscription ( ) {
292+ this . _defineSortingDataAccessor ( ) ;
293+
270294 // Sorting and/or pagination should be watched if MatSort and/or MatPaginator are provided.
271295 // The events should emit whenever the component emits a change or initializes, or if no
272296 // component is provided, a stream with just a null event should be provided.
@@ -385,6 +409,8 @@ export class _MatTableDataSource<
385409 * @docs -private
386410 */
387411 connect ( ) {
412+ this . _defineSortingDataAccessor ( ) ;
413+
388414 if ( ! this . _renderChangesSubscription ) {
389415 this . _updateChangeSubscription ( ) ;
390416 }
0 commit comments