WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 2 weeks ago

#41549 assigned enhancement

REST API: Use `wp.apiRequest` helper in `wp.api` Backbone client

Reported by: jnylen0 Owned by: adamsilverstein
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: REST API Keywords: needs-patch needs-unit-tests dev-feedback
Focuses: javascript, administration, rest-api Cc:

Description

Follow-up to #40919. The wp-api.js Backbone client should use wp.apiRequest to do its API calls. This may be accomplished through overriding Backbone.ajax or through a change in the wp-api.js code.

This will allow client-side code to override or modify all WP REST API calls in a single, centralized place.

cc @adamsilverstein for your thoughts on the best way to achieve this.

Attachments (1)

41549.diff (2.1 KB) - added by adamsilverstein 5 months ago.

Download all attachments as: .zip

Change History (6)

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


5 months ago

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


5 months ago

#3 @adamsilverstein
5 months ago

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

#4 @adamsilverstein
5 months ago

  • Keywords dev-feedback added
  • Milestone changed from 4.9 to Future Release

@jnylen0 - 41549.diff is a first pass at this.

  • Override Backbone.ajax with wp.apiRequest.transport - I looked at replacing first with wp.apiRequest, however I don't think we need the additional logic in buildAjaxOptions and this might break something.
  • Immediately restore Backbone.ajax after use
  • Adjust tests to override wp.apiRequest.transport for testing

Not sure I like this - my concern here is that Backbone authors are used to the idea of being able to override Backbone.ajax to use an alternate method, for example this native implementation. This would actually break that (for the client), requiring that they override wp.apiRequest.transport instead.

Punting for now for further consideration.

#5 @jnylen0
2 weeks ago

@adamsilverstein I think most of the value in this change is in having wp-api.js use buildAjaxOptions. I could have explained the reasoning better in the ticket, but here is a good example of how this could be helpful: #42807

I am not sure how to handle users overriding Backbone.ajax. It would be good to see an example of this in WordPress context, otherwise I think it is probably not very common.

Note: See TracTickets for help on using tickets.