REST API: Add error handling for fetch error in buildModelGetter of wp-api.js.

When a call to a model getter method fails, reject the returned deferred object. Enables better handling of fetch errors.

Props westonruter, adamsilverstein.
Fixes #39314.

Built from https://develop.svn.wordpress.org/trunk@39680


git-svn-id: http://core.svn.wordpress.org/trunk@39620 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2017-01-05 03:03:42 +00:00
parent e2166de4b0
commit c8a3496ebe
3 changed files with 21 additions and 11 deletions

View File

@ -321,12 +321,17 @@
// Create the new getModel model.
getModel = new wp.api.models[ modelName ]( attributes );
// If we didnt have an embedded getModel, fetch the getModel data.
if ( ! getModel.get( embedCheckField ) ) {
getModel.fetch( { success: function( getModel ) {
deferred.resolve( getModel );
} } );
getModel.fetch( {
success: function( getModel ) {
deferred.resolve( getModel );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {
// Resolve with the embedded model.
deferred.resolve( getModel );
}
@ -392,12 +397,17 @@
// If we didnt have embedded getObjects, fetch the getObjects data.
if ( _.isUndefined( getObjects.models[0] ) ) {
getObjects.fetch( { success: function( getObjects ) {
getObjects.fetch( {
success: function( getObjects ) {
// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
} } );
// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {
// Add a helper 'parent_post' attribute onto the model.

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8-alpha-39678';
$wp_version = '4.8-alpha-39680';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.