Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#41111 closed enhancement (fixed)

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

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

Description

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 9 years ago.
Add helper methods and unit tests
41111.2.diff​ (3.1 KB ) - added by adamsilverstein 9 years ago.
41111.3.diff​ (3.1 KB ) - added by adamsilverstein 9 years ago.

Download all attachments as: .zip

Change History (12)

#1 @adamsilverstein
9 years ago

  • Owner set to adamsilverstein
  • Status new → assigned

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


9 years ago

@rcutmore
9 years ago

Add helper methods and unit tests

#3 @rcutmore
9 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
9 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
9 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
9 years ago

41111.3.diff​

  • remove some trailing commas to make jshint happy

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

https://cl.ly/0T0I2W2m2m1W/Image%202017-08-18%20at%2010.55.57%20AM.png

cc: @netweb 

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


9 years ago

#8 @adamsilverstein
9 years ago

  • Resolution → fixed
  • Status assigned → 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.


9 years ago

Note: See TracTickets for help on using tickets.