Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#41111 closed enhancement (fixed)

WP-API JS Client: Add a helper to get model or collection by route

Reported by: adamsilverstein's profile adamsilverstein Owned by: adamsilverstein's profile adamsilverstein
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: good-first-bug has-patch has-unit-tests commit
Focuses: javascript Cc:


In the process of adding page/custom post type support to Gutenberg, the need arose to identify the models based on their route - the code passes the route from PHP to JavaScript, and the JavaScript needs to know which model to use. See this pull requst for additional details.

We should add helpers to the client to easily find a model or collection based on its route, something like getModelByRoute and getCollectionByRoute - each would search thru the constructed models/collections and return the matching object, or false if none was found. We should also add unit tests to verify the functions work as expected.

Attachments (3)

41111.diff (3.0 KB) - added by rcutmore 7 years ago.
Add helper methods and unit tests
41111.2.diff (3.1 KB) - added by adamsilverstein 7 years ago.
41111.3.diff (3.1 KB) - added by adamsilverstein 7 years ago.

Download all attachments as: .zip

Change History (12)

#1 @adamsilverstein
7 years ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core-js by adamsilverstein. View the logs.

7 years ago

7 years ago

Add helper methods and unit tests

#3 @rcutmore
7 years ago

  • Keywords needs-patch needs-unit-tests removed

Hi @adamsilverstein

I added 41111.diff to add these helper methods as well as the associated unit tests. Let me know if this is on the right track or if I'm missing anything. I wasn't sure exactly where the helper methods should be added.

#4 @adamsilverstein
7 years ago

  • Keywords has-patch has-unit-tests reporter-feedback added

@rcutmore looks good at first glance, I will take a closer look soon. Thanks for your work here!

#5 @adamsilverstein
7 years ago

  • Keywords commit added; reporter-feedback removed

@rcutmore - nice work here!

In 41111.2.diff I moved the code up a bit into the utility section, ultimately we will probably split this file back up so wanted to group the functions with the other helpers. Also, I clarified the return value would be undefined if not matching item is found and I switched out _.first( _.filter(... for _.find(... which should also return the first item matching the predicate. This makes the code a little more legible and all the unit tests continued to pass.

#6 @adamsilverstein
7 years ago


  • remove some trailing commas to make jshint happy

Question: do we still need these tests if we no longer support older versions of IE?

cc: @netweb 

This ticket was mentioned in Slack in #core-js by netweb. View the logs.

7 years ago

#8 @adamsilverstein
7 years ago

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

In 41334:

WP-API JS Client: Add helpers to get a model or collection by route.

Add two new helper functions, wp.api.getModelByRoute and wp.api.getCollectionByRoute. Passed a route, they return the matching model or collection, or undefined if none is found.

Also adds tests to verify these functions work as expected.

Props rcutmore.
Fixes #41111.

This ticket was mentioned in Slack in #core-js by aduth. View the logs.

7 years ago

Note: See TracTickets for help on using tickets.