ajax - jQuery and couchdb: is it possible to handle "created" and "already exist" in one handler? -
a typical pattern no use rest couchdb api "create if not exist". example, if want create database if it's not exist:
$.ajax({ type: 'put', url: db + 'mydatabase', });
as programmer, both 201 created
, 412 prerequisite failed
success, since want database in place , it's fine me if it's created or here. jquery
perspective, 201
success , 412
failure - need write lots of code make sure database in place:
$.ajax({ type: 'put', url: db + 'mydatabase', }).fail( function( arg ) { if( 412 == arg.statuscode ) { // success. } else { // failure. } }).done( function( arg ) { // success. });
this makes code mess success in 2 places (even in 2 different callbacks!). possible somehow reduce error handling in less code, preferably processing success in 1 place?
alternatively can use statuscode
object bind shared event handler 200 , 412.
$.ajax({ type: 'put', url: db + 'mydatabase', statuscode: { 200: myhandler, 412: myhandler } }); function myhandler(data, statustext, jqxhr) { // data }
for completeness here's other way described in comment.
$.ajax({ type: 'put', url: db + 'mydatabase', complete: function(jqxhr) { if (jqxhr.status === 200 || jqxhr.status === 412) { // } return; } });
Comments
Post a Comment