@@ -19,9 +19,12 @@ class GenericModel {
1919 return Reflect . deleteProperty ( target , propertyKey )
2020 } ,
2121 get : ( target , propertyKey , receiver ) => {
22- if ( propertyKey === 'constructor' || Reflect . ownKeys ( this ) . includes ( propertyKey ) ) {
22+ if ( propertyKey === 'constructor' ) {
2323 return Reflect . get ( this , propertyKey , this )
2424 }
25+ else if ( Reflect . ownKeys ( this ) . includes ( propertyKey ) ) {
26+ return Reflect . get ( this , propertyKey , this ) . bind ( this , receiver )
27+ }
2528 return Reflect . get ( target , propertyKey , receiver )
2629 } ,
2730 has : ( target , propertyKey ) => {
@@ -34,7 +37,7 @@ class GenericModel {
3437 } )
3538 }
3639
37- async #request ( { method, keepChanges = false , needsQuery = true } , signal , opts , ...data ) {
40+ async #request ( receiver , { method, keepChanges = false , needsQuery = true } , signal , opts , ...data ) {
3841 await this . #options. route . $ready
3942 const { columns, ...options } = opts
4043
@@ -79,32 +82,32 @@ class GenericModel {
7982 // TODO: do we need to delete missing keys?
8083 if ( keepChanges ) {
8184 const diff = this . #proxy[ $diff ]
82- Object . entries ( body ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
85+ Object . entries ( body ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8386 this . #proxy[ $freeze ] ( )
84- Object . entries ( diff ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
87+ Object . entries ( diff ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8588 } else {
86- Object . entries ( body ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
89+ Object . entries ( body ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8790 this . #proxy[ $freeze ] ( )
8891 }
8992 return body
9093 }
9194
92- $get = new ObservableFunction ( async ( signal , opts = { } ) => {
95+ $get = new ObservableFunction ( async ( receiver , signal , opts = { } ) => {
9396 const { keepChanges, ...options } = opts
94- return this . #request( { method : 'get' , keepChanges } , signal , options )
97+ return this . #request( receiver , { method : 'get' , keepChanges } , signal , options )
9598 } )
9699
97- $post = new ObservableFunction ( async ( signal , opts = { } ) => {
100+ $post = new ObservableFunction ( async ( receiver , signal , opts = { } ) => {
98101 const options = { return : 'representation' , ...opts }
99- return this . #request( { method : 'post' , needsQuery : false } , signal , options , this . #proxy)
102+ return this . #request( receiver , { method : 'post' , needsQuery : false } , signal , options , this . #proxy)
100103 } )
101104
102- $put = new ObservableFunction ( async ( signal , opts ) => {
105+ $put = new ObservableFunction ( async ( receiver , signal , opts ) => {
103106 const options = { return : 'representation' , ...opts }
104- return this . #request( { method : 'put' } , signal , options , this . #proxy)
107+ return this . #request( receiver , { method : 'put' } , signal , options , this . #proxy)
105108 } )
106109
107- $patch = new ObservableFunction ( async ( signal , opts , data = { } ) => {
110+ $patch = new ObservableFunction ( async ( receiver , signal , opts , data = { } ) => {
108111 const options = { return : 'representation' , ...opts }
109112
110113 if ( ! data || typeof data !== 'object' ) {
@@ -121,11 +124,11 @@ class GenericModel {
121124 return this . #proxy
122125 }
123126
124- return this . #request( { method : 'patch' } , signal , options , patchData )
127+ return this . #request( receiver , { method : 'patch' } , signal , options , patchData )
125128 } )
126129
127- $delete = new ObservableFunction ( async ( signal , options = { } ) => {
128- return this . #request( { method : 'delete' } , signal , options )
130+ $delete = new ObservableFunction ( async ( receiver , signal , options = { } ) => {
131+ return this . #request( receiver , { method : 'delete' } , signal , options )
129132 } )
130133}
131134
0 commit comments