Wikodit / js-data-jsonapi-light

JSData adapter which serialize and deserialize JSONApi response and requests.
MIT License
8 stars 4 forks source link

Remove the need to give the store as option #15

Closed Tronix117 closed 7 years ago

Tronix117 commented 7 years ago

Chat extract about this matter:

tronix117 [8:27 AM] Hey guys, I'm working on a v3 adapter and I am trying to find a proper way to retrieve a mapper. I have access to a mapper and to the adapter instance, and would like to retrieve another mapper from the same store. When using v2, it was easy : Resource.getResource(), however on v3, my only workaround is forcing the user to give its store instance as an option of the adapter, which I doesn't like...

coryrobinson42 [2 days ago]
@tronix117 do you have a gist or code to share? (edited)

tronix117 [2 days ago]
coryrobinson42: https://github.com/Wikodit/js-data-jsonapi-light/blob/develop/src/deserializer.ts#L46

tronix117 [2 days ago]
I'm creating an adapter and need to initialize this way : ⁠⁠⁠⁠const jsonApiAdapter = new JsonApiAdapter({store: store})⁠⁠⁠⁠ which I don't find very clean

coryrobinson42 [2 days ago]
Have you checked out the other adapters? Store is passed through the adapter as long as it's been registered with the store you're concerned with

coryrobinson42 [2 days ago]
https://github.com/js-data/js-data-http/blob/3.0.0-rc.2/src/index.js

coryrobinson42 [2 days ago]
I could be wrong, it looks like you can circumvent passing the store with your adapter instantiation.

tronix117 [2 days ago]
Are you sure ? Because how I see it in the code, the store calls adapters methods, but is never linked back to it, usualy the mapper doesn't need to know its store

tronix117 [2 days ago]
however in my case, the JsonApi structure needs an access to other mappers, thus an access to the common link between them : the store

tronix117 [2 days ago]
unless I missed something

coryrobinson42 [2 days ago]
hmm... I'll check it out a bit more later today and see if I can help more

tronix117 [2 days ago]
Thank you I appreciate it, unfortunately I don't think I have a proper way to do that, maybe @jmdobry can confirm that.

jmdobry [1 day ago]
Mappers defined via ⁠⁠⁠⁠defineMapper⁠⁠⁠⁠ have a reference to the store that defined them at ⁠⁠⁠⁠Mapper#datastore⁠⁠⁠⁠.

jmdobry [1 day ago]
https://github.com/js-data/js-data/blob/master/src/Container.js#L1176

jmdobry [1 day ago]
When A Mapper calls Adapter methods, the Mapper passes a reference to itself as the first argument, therefore, Adapter methods have access to the Mapper’s datastore

tronix117 [21 hours ago]
how did I missed that... Thanks a lot !