Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#39070 closed defect (bug) (fixed)

WP-API JS client can't use getCategories for models returned by collections

Reported by: jesseenterprises's profile jesseenterprises Owned by: adamsilverstein's profile 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)

39070.diff (992 bytes) - added by jesseenterprises 8 years ago.

Download all attachments as: .zip

Change History (12)

#1 @jesseenterprises
8 years ago

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 the model property of fetched collections as a function returning an instantiated model, instead of a static model prototype.

#2 @adamsilverstein
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

#4 @helen
8 years ago

  • Milestone changed from Awaiting Review to 4.7.1

Milestoning for investigation.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

#6 @adamsilverstein
8 years ago

  • Keywords needs-unit-tests added

#7 @adamsilverstein
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.

#8 @adamsilverstein
8 years ago

  • Keywords commit added; needs-testing needs-unit-tests removed

This ticket was mentioned in Slack in #core by jorbin. View the logs.


8 years ago

#10 @adamsilverstein
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 39603:

WP-API: JavaScript client - fix setup of models used by wp.api.collections objects.

Correct setup for the model attribute of wp.api.collections objects. Set the collection model as a function that returns a new model of the underlying type, instead of setting it as the model prototype. Fixes an issue where models for fetched collections weren't set up properly and didn't have the expected mixin methods such as getCategories available.

Props jesseenterprises.
Fixes #39070.

#11 @adamsilverstein
8 years ago

In 39604:

WP-API: JavaScript client - fix setup of models used by wp.api.collections objects.

Correct setup for the model attribute of wp.api.collections objects. Set the collection model as a function that returns a new model of the underlying type, instead of setting it as the model prototype. Fixes an issue where models for fetched collections weren't set up properly and didn't have the expected mixin methods such as getCategories available.

Props jesseenterprises.
Merges [39603] to the 4.7 branch.
Fixes #39070.

Note: See TracTickets for help on using tickets.