#39070 closed defect (bug) (fixed)
WP-API JS client can't use getCategories for models returned by collections
Reported by: | jesseenterprises | Owned by: | adamsilverstein |
---|---|---|---|
Milestone: | 4.7.1 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch commit |
Focuses: | javascript, rest-api | Cc: |
Description
Migrating report from https://github.com/WP-API/client-js/issues/144
If I do this:
var postsCollection = new wp.api.collections.Posts(); postsCollection.fetch();
I'm only seeing the category id returned in the attributes object for each post.
Is there a way to include all category data there? Or from that point, when I'm looping over postsCollection, how do I get the category data for each post?
I tried doing this:
postsCollection.each( function( post ) { post.getCategories().done( function( postCategories ) { // ... do something with the categories. // The new post has an single Category: Uncategorized console.log( postCategories[0].name ); // response -> "Uncategorized" }); });
But this is giving me post.getCategories is not a function.
Any insight here? Basically I'm just trying to find out how to loop over posts AND include their categories.
Attachments (1)
Change History (12)
#2
@
8 years ago
- Keywords has-patch needs-testing added
- Owner set to adamsilverstein
- Status changed from new to assigned
Looks good @jesseenterprises, thanks! I'll test this out.
This ticket was mentioned in Slack in #core by helen. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#7
@
8 years ago
I added some additional tests for this in https://github.com/WP-API/client-js/pull/148/files. I am still working on getting these tests integrated into the WordPress core testing suit. I did verify they fail before the attached patch and pass after it.
39070.diff fixes an issue where models automatically created as children of fetched collections didn't have appropriate mixin methods, like
.getCategories
, applied. This is resolved by defining themodel
property of fetched collections as a function returning an instantiated model, instead of a static model prototype.