简体   繁体   中英

How to get dynamic segment id inside a hook in the Ember REST adapter

Assuming we have URL like /posts/awesome-post/comments (which is /posts/1/comments which is /posts/:post_id/comments )

We have comment adapter

export default DS.RESTAdapter.extend({
  pathForType(modelName) {
    // how to get :post_id here?
  }
});

How to get Awesome Post's id (:post_id) inside pathForType hook in the comment adapter?

You likely want to use store.find in this scenario. So in your adapter, override the buildURL method like this:

export default DS.RESTAdapter.extend({
    buildURL(modelName, id, snapshot, requestType, query) {
        const postId = query.postId;

        if (postId) {
            // Make your own URL
        } else {
            return this._super.apply(this, arguments);
        }
    }
});

Then you can fetch the comments like this:

this.store.find('comment', { postId: id });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM