Skip to content
This repository was archived by the owner on Sep 5, 2018. It is now read-only.

Embeds in collections #57

Closed
colinhowells opened this issue Dec 27, 2015 · 6 comments
Closed

Embeds in collections #57

colinhowells opened this issue Dec 27, 2015 · 6 comments

Comments

@colinhowells
Copy link

Is it possible to set _embed when using this? I gather that embeds are on for collections, but I'm unclear on what the syntax would be here; not sure if there's something like var postsCollection = new wp.api.collections.Posts( {}. { embed: 'yup' } ); and embeds seem to be off by default.

@adamsilverstein
Copy link

@colinhowells

Thanks for the question. The docs are a little weak here, I will try to improve that.

You should be able to get post _embed data directly from collections:

var posts = new wp.api.collections.Posts()
posts.fetch( { data: { _embed: true } } );

Here is how this looks in my console, with this parameter passed, the api returns all post meta data:

@colinhowells
Copy link
Author

Ahhhhh it was staring me right in the face the whole time. Thanks, I'm still learning all this.

One question though: while now I can get to, say, post._embedded.author[0].name I can't get ahold of post._embedded.['https://api.w.org/featuredmedia'][0].media_details.sizes.medium.source_url due to the URL in there. I've tried it a few ways but I run into errors each time, I know there's something obvious I'm missing ...

@adamsilverstein
Copy link

@colinhowells:
I am able to get this with
postsCollection.models[0].attributes._embedded['https://api.w.org/featuredmedia'][0].media_details.sizes.medium.source_url

You could also use postsCollection.models[0].get( '_embedded' )['https://api.w.org/featuredmedia'][0].media_details.sizes.medium.source_url

What happens when you try that?

@colinhowells
Copy link
Author

Turns out I had a dot in there I didn't need. I had _embedded.['https://api.w.org/featuredmedia'] but what I needed was _embedded['https://api.w.org/featuredmedia'] I wasn't expecting that, since _embedded.author[0] needs that dot. Evidently closing the URL in brackets serves the same function, which I didn't know.

@adamsilverstein
Copy link

Cool, marking this as resolved 👍 Thanks for building on this library!

Please share your project with the rest-api team when/if its in a public place!

@colinhowells
Copy link
Author

Thanks for taking the time to help, I appreciate it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants