#41111 closed enhancement (fixed)
WP-API JS Client: Add a helper to get model or collection by route
Reported by: | adamsilverstein | Owned by: | 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: |
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)
Change History (12)
This ticket was mentioned in Slack in #core-js by adamsilverstein. View the logs.
7 years ago
#3
@
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
@
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
@
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
@
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
Add helper methods and unit tests