@@ -19,8 +19,10 @@ 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 )
24+ } else if ( Reflect . ownKeys ( this ) . includes ( propertyKey ) ) {
25+ return Reflect . get ( this , propertyKey , this ) . bind ( this , receiver )
2426 }
2527 return Reflect . get ( target , propertyKey , receiver )
2628 } ,
@@ -34,7 +36,7 @@ class GenericModel {
3436 } )
3537 }
3638
37- async #request ( { method, keepChanges = false , needsQuery = true } , signal , opts , ...data ) {
39+ async #request ( receiver , { method, keepChanges = false , needsQuery = true } , signal , opts , ...data ) {
3840 await this . #options. route . $ready
3941 const { columns, ...options } = opts
4042
@@ -79,37 +81,37 @@ class GenericModel {
7981 // TODO: do we need to delete missing keys?
8082 if ( keepChanges ) {
8183 const diff = this . #proxy[ $diff ]
82- Object . entries ( body ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
84+ Object . entries ( body ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8385 this . #proxy[ $freeze ] ( )
84- Object . entries ( diff ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
86+ Object . entries ( diff ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8587 } else {
86- Object . entries ( body ) . forEach ( ( [ key , value ] ) => { this . #proxy [ key ] = value } )
88+ Object . entries ( body ) . forEach ( ( [ key , value ] ) => { receiver [ key ] = value } )
8789 this . #proxy[ $freeze ] ( )
8890 }
8991 return body
9092 }
9193
92- $get = new ObservableFunction ( async ( signal , opts = { } ) => {
94+ $get = new ObservableFunction ( async ( receiver , signal , opts = { } ) => {
9395 const { keepChanges, ...options } = opts
94- return this . #request( { method : 'get' , keepChanges } , signal , options )
96+ return this . #request( receiver , { method : 'get' , keepChanges } , signal , options )
9597 } )
9698
97- $post = new ObservableFunction ( async ( signal , opts = { } ) => {
99+ $post = new ObservableFunction ( async ( receiver , signal , opts = { } ) => {
98100 const options = { return : 'representation' , ...opts }
99- const body = await this . #request( { method : 'post' , needsQuery : false } , signal , options , this . #proxy)
101+ const body = await this . #request( receiver , { method : 'post' , needsQuery : false } , signal , options , this . #proxy)
100102 if ( body ) {
101103 // we need to make sure the query is updated with the primary key
102104 this . #options. query = createPKQuery ( this . #options. route . pks , mapAliasesFromSelect ( this . #options. query ?. select , body ) )
103105 }
104106 return body
105107 } )
106108
107- $put = new ObservableFunction ( async ( signal , opts ) => {
109+ $put = new ObservableFunction ( async ( receiver , signal , opts ) => {
108110 const options = { return : 'representation' , ...opts }
109- return this . #request( { method : 'put' } , signal , options , this . #proxy)
111+ return this . #request( receiver , { method : 'put' } , signal , options , this . #proxy)
110112 } )
111113
112- $patch = new ObservableFunction ( async ( signal , opts , data = { } ) => {
114+ $patch = new ObservableFunction ( async ( receiver , signal , opts , data = { } ) => {
113115 const options = { return : 'representation' , ...opts }
114116
115117 if ( ! data || typeof data !== 'object' ) {
@@ -126,11 +128,11 @@ class GenericModel {
126128 return this . #proxy
127129 }
128130
129- return this . #request( { method : 'patch' } , signal , options , patchData )
131+ return this . #request( receiver , { method : 'patch' } , signal , options , patchData )
130132 } )
131133
132- $delete = new ObservableFunction ( async ( signal , options = { } ) => {
133- return this . #request( { method : 'delete' } , signal , options )
134+ $delete = new ObservableFunction ( async ( receiver , signal , options = { } ) => {
135+ return this . #request( receiver , { method : 'delete' } , signal , options )
134136 } )
135137}
136138
0 commit comments