我是JsonRestStores的作者。我已经把这个问题推迟了太久了。这是一个将赢得“年度最愚蠢的缩进函数”奖的函数。
主要的棘手问题是闭包变量发生了变化:
body[ self.idProperty ] = params[ self.idProperty ];
还有一个“如果”让事情变得有趣。
那么......有没有一种优雅的方法可以将此功能变成看起来不像带有两个戳的箭头的东西?如果是这样,您能否提供一个示例实现?
_makePostAppend: function( params, body, options, next ){
var self = this;
var body;
if( typeof( next ) !== 'function' ) next = function(){};
// Check that the method is implemented
if( ! self.handlePostAppend ){
self._sendError( next, new self.NotImplementedError( ) );
return;
}
// Check the IDs
self._checkParamIds( params, body, false, function( err ){
self._sendErrorOnErr( err, next, function(){
self.schema.validate( body, function( err, body, errors ) {
self._sendErrorOnErr( err, next, function(){
if( errors.length ){
self._sendError( next, new self.UnprocessableEntityError( { errors: errors } ) );
} else {
// Fetch the doc
self.execAllDbFetch( params, body, options, function( err, fullDoc ){
self._sendErrorOnErr( err, next, function(){
self.extrapolateDoc( params, body, options, fullDoc, function( err, doc) {
self._sendErrorOnErr( err, next, function(){
self._castDoc( doc, function( err, doc) {
self._sendErrorOnErr( err, next, function(){
// Actually check permissions
self.checkPermissionsPostAppend( params, body, options, doc, fullDoc, function( err, granted ){
self._sendErrorOnErr( err, next, function(){
if( ! granted ){
self._sendError( next, new self.ForbiddenError() );
} else {
// Clean up body from things that are not to be submitted
//if( self.schema ) self.schema.cleanup( body, 'doNotSave' );
self.schema.cleanup( body, 'doNotSave' );
// Paranoid check
// Make sure that the id property in the body does match
// the one passed as last parameter in the list of IDs
body[ self.idProperty ] = params[ self.idProperty ];
self.execPostDbAppend( params, body, options, doc, fullDoc, function( err, fullDocAfter ){
self._sendErrorOnErr( err, next, function(){
self.extrapolateDoc( params, body, options, fullDocAfter, function( err, doc) {
self._sendErrorOnErr( err, next, function(){
self._castDoc( fullDocAfter, function( err, docAfter) {
self._sendErrorOnErr( err, next, function(){
// Remote request: set headers, and send the doc back (if echo is on)
if( self.remote ){
if( self.echoAfterPostAppend ){
self.prepareBeforeSend( docAfter, function( err, docAfter ){
self._sendErrorOnErr( err, next, function(){
self.afterPostAppend( params, body, options, doc, fullDoc, docAfter, fullDocAfter, function( err ){
self._sendErrorOnErr( err, next, function(){
self._res.json( 200, docAfter );
});
});
})
})
} else {
self.afterPostAppend( params, body, options, doc, fullDoc, docAfter, fullDocAfter, function( err ){
self._sendErrorOnErr( err, next, function(){
self._res.send( 204, '' );
});
});
}
// Local request: simply return the doc to the asking function
} else {
self.prepareBeforeSend( docAfter, function( err, docAfter ){
self._sendErrorOnErr( err, next, function(){
self.afterPostAppend( params, body, options, doc, fullDoc, docAfter, fullDocAfter, function( err ){
self._sendErrorOnErr( err, next, function(){
next( null, docAfter, self.idProperty );
})
})
})
})
}
})
});
});
})
}) // err
}) // execPostDbAppend
} // granted
})
})
})
})
})
})
}) // err
}) // checkPermissionsPostAppend
} // errors.length
}) // err
}) // self.validate
}) // err
}) // self.validate
},