2 * jqCouchDB - jQuery plugin for couchdb connections
3 * @requires jQuery > v1.0.3
5 * http://protoblogr.net
7 * Copyright (c) 2007 Jerry Jalava (protoblogr.net)
8 * Dual licensed under the MIT and GPL licenses:
9 * http://www.opensource.org/licenses/mit-license.php
10 * http://www.gnu.org/licenses/gpl.html
17 var db_exists = false;
18 var jqcouch_db = $.jqcouch.db;
20 jqcouch_db.on_error = function(request, caller) { db_exists = false; };
21 jqcouch_db.on_success = function(data, caller) { db_exists = data.exists; };
22 jqcouch_db.exists('/some_database');
26 $.jqcouch.db.create('/some_database');
29 jqcouch_db.on_success = function(data, caller)
33 jqcouch_db.info('/some_database');
37 * You don't have to override the on_error and on_success methods if you don't wan't to...
40 $.jqcouch.doc.save('/some_database/note1', {
44 $.jqcouch.doc.on_success = function(data, caller) {
46 $.jqcouch.doc.on_success = $.jqcouch.doc._original_on_success;
48 $.jqcouch.doc.get('/some_database/note1');
51 * You could also override the global function:
53 $.jqcouch.on_error = function(request,caller)
55 some_other_error_func(request, caller);
57 $.jqcouch.on_success = function(data,caller)
59 some_other_success_func(data, caller);
65 $.jqcouch = $.jqcouch || {};
67 $.jqcouch.parseJSON = function (json_str)
71 return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
72 json_str.replace(/"(\\.|[^"\\])*"/g, ''))) &&
73 eval('(' + json_str + ')');
80 $.jqcouch.toJSON = function (item,item_type)
93 var a = ['['], b, f, i, l = x.length, v;
94 for (i = 0; i < l; i += 1) {
97 if (typeof v == 'string') {
115 return isFinite(x) ? String(x) : 'null';
119 if (x instanceof Array) {
122 var a = ['{'], b, f, i, v;
126 if (typeof v == 'string') {
130 a.push(s.str(i), ':', v);
140 if (/["\\\x00-\x1f]/.test(x)) {
141 x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
148 Math.floor(c / 16).toString(16) +
149 (c % 16).toString(16);
152 return '"' + x + '"';
155 conv = function (x) {
156 var itemtype = typeof x;
179 var itemtype = item_type || typeof item;
200 throw("Unknown type for $.jqcouch.toJSON");
204 $.jqcouch.on_error = function(request, caller)
206 var error = "Error occured while talking with server!\n";
207 error += "request.status: "+request.status+"\n";
209 if (caller != undefined)
211 error += "caller.method: "+caller.method+"\n";
212 error += "caller.action: "+caller.action+"\n";
213 if (caller.args != undefined)
215 error += "caller.args: "+caller.args.toString()+"\n";
219 //error += "request.responseText: "+request.responseText+"\n";
221 var response = eval("(" + request.responseText + ")");
222 error += "response.error.reason: "+response.error.reason+"\n";
224 $.jqcouch.on_success = function(data, caller)
226 alert("on_success method is not specified!");
229 $.jqcouch.generate_query_str = function(query_args)
231 var query_string = '';
232 if (query_args != undefined)
235 $.each(query_args, function(key,value){
236 query_string += key + '=' + value + '&';
238 query_string = query_string.substr(0,query_string.length-1);
244 $.jqcouch.server_url = '/';
246 $.jqcouch.ajax_setup = {
252 contentType: 'application/json'
256 exists: function(path)
259 var server_url = $.jqcouch.server_url;
261 url: server_url + '_all_dbs',
267 contentType: 'application/json',
268 error: function(request){
269 $.jqcouch.db.on_error(request, {
270 method: 'jqcouch.db.exists',
276 success: function(data){
277 $.each(data, function(i,n){
283 $.jqcouch.db.on_success({exists: exists}, {
284 method: 'jqcouch.db.exists',
294 var server_url = $.jqcouch.server_url;
296 url: server_url + '_all_dbs',
302 contentType: 'application/json',
303 error: function(request){
304 $.jqcouch.db.on_error(request, {
305 method: 'jqcouch.db.exists',
311 success: function(data){
312 $.jqcouch.db.on_success({exists: exists}, {
313 method: 'jqcouch.db.info',
321 create: function(path)
323 if (path.substr(path.length-1,1) != '/')
328 var server_url = $.jqcouch.server_url;
330 url: server_url + path,
336 contentType: 'application/json',
337 error: function(request){
338 $.jqcouch.db.on_error(request, {
339 method: 'jqcouch.db.create',
345 success: function(data){
346 $.jqcouch.db.on_success(data, {
347 method: 'jqcouch.db.create',
355 remove: function(path)
357 if (path.substr(path.length-1,1) != '/')
362 var server_url = $.jqcouch.server_url;
364 url: server_url + path,
370 contentType: 'application/json',
371 error: function(request){
372 $.jqcouch.db.on_error(request, {
373 method: 'jqcouch.db.remove',
379 success: function(data){
380 $.jqcouch.db.on_success(data, {
381 method: 'jqcouch.db.remove',
391 var server_url = $.jqcouch.server_url;
393 url: server_url + path,
399 contentType: 'application/json',
400 error: function(request){
401 $.jqcouch.db.on_error(request, {
402 method: 'jqcouch.db.info',
408 success: function(data){
409 $.jqcouch.db.on_success(data, {
410 method: 'jqcouch.db.info',
419 on_error: function(request, caller)
421 $.jqcouch.on_error(request, caller);
423 on_success: function(data, caller)
425 $.jqcouch.on_success(data, caller);
427 _original_on_error: this.on_error,
428 _original_on_success: this.on_success
432 get: function(path, query_args)
434 path += $.jqcouch.generate_query_str(query_args);
436 var server_url = $.jqcouch.server_url;
438 url: server_url + path,
444 contentType: 'application/json',
445 error: function(request){
446 $.jqcouch.doc.on_error(request, {
447 method: 'jqcouch.doc.get',
453 success: function(data){
454 $.jqcouch.doc.on_success(data, {
455 method: 'jqcouch.doc.get',
463 get_all: function(path, query_args)
465 if (path.substr(path.length-1,1) != '/')
470 path += $.jqcouch.generate_query_str(query_args);
472 var server_url = $.jqcouch.server_url;
474 url: server_url + path,
480 contentType: 'application/json',
481 error: function(request){
482 $.jqcouch.doc.on_error(request, {
483 method: 'jqcouch.doc.get_all',
485 args: {query_args: query_args}
489 success: function(data){
490 $.jqcouch.doc.on_success(data, {
491 method: 'jqcouch.doc.get_all',
493 args: {query_args: query_args}
499 save: function(path, data)
501 if ( data._id == undefined
504 if (path.substr(path.length-1,1) != '/')
508 $.jqcouch.doc.post(path, data);
514 data._rev = Number(data._rev);
516 if (path.substr(path.length-1,1) != '/')
520 $.jqcouch.doc.put(path + data._id, data);
523 put: function(path, data)
525 var server_url = $.jqcouch.server_url;
527 url: server_url + path,
529 data: $.jqcouch.toJSON(data),
535 contentType: 'application/json',
536 error: function(request){
537 $.jqcouch.doc.on_error(request, {
538 method: 'jqcouch.doc.put',
544 success: function(data){
545 $.jqcouch.doc.on_success(data, {
546 method: 'jqcouch.doc.put',
554 post: function(path, data)
556 var server_url = $.jqcouch.server_url;
558 url: server_url + path,
564 contentType: 'application/json',
565 data: $.jqcouch.toJSON(data),
567 error: function(request){
568 $.jqcouch.doc.on_error(request, {
569 method: 'jqcouch.doc.post',
571 args: {path: path, data: data}
575 success: function(data){
576 $.jqcouch.doc.on_success(data, {
577 method: 'jqcouch.doc.post',
579 args: {path: path, data: data}
585 del: function(path, revision)
587 if (revision != undefined)
589 path += '?rev=' + revision;
592 var server_url = $.jqcouch.server_url;
594 url: server_url + path,
600 contentType: 'application/json',
601 error: function(request){
602 $.jqcouch.doc.on_error(request, {
603 method: 'jqcouch.doc.del',
609 success: function(data){
610 $.jqcouch.doc.on_success(data, {
611 method: 'jqcouch.doc.del',
620 on_error: function(request, caller)
622 $.jqcouch.on_error(request, caller);
624 on_success: function(data, caller)
626 $.jqcouch.on_success(data, caller);
628 _original_on_error: this.on_error,
629 _original_on_success: this.on_success
633 exists: function(path, name)
638 if (path.substr(path.length-1,1) != '/')
644 var server_url = $.jqcouch.server_url;
646 url: server_url + path,
652 contentType: 'application/json',
653 error: function(request){
654 $.jqcouch.view.on_error(request, {
655 method: 'jqcouch.view.exists',
661 success: function(data){
662 $.each(data.rows, function(i,n){
669 $.jqcouch.view.on_success({exists: exists, rev: rev}, {
670 method: 'jqcouch.view.exists',
678 get: function(path, query_args)
680 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
681 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
682 $.jqcouch.doc.get(path, query_args);
684 save: function(path, data)
686 if ( data._id == undefined
692 if ( data._rev == undefined
695 if (path.substr(path.length-1,1) != '/')
699 $.jqcouch.view.post(path, data);
703 data._rev = Number(data._rev);
704 if (path.substr(path.length-1,1) != '/')
708 $.jqcouch.view.put(path + data._id, data);
711 post: function(path, data)
713 var server_url = $.jqcouch.server_url;
715 url: server_url + path,
721 contentType: 'application/json',
722 data: $.jqcouch.toJSON(data),
724 error: function(request){
725 $.jqcouch.view.on_error(request, {
726 method: 'jqcouch.doc.post',
732 success: function(data){
733 $.jqcouch.view.on_success(data, {
734 method: 'jqcouch.doc.post',
742 put: function(path, data)
744 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
745 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
746 $.jqcouch.doc.put(path, data);
750 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
751 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
752 $.jqcouch.doc.del(path);
754 temp: function(path, query_args, map)
756 if (path.substr(path.length-1,1) != '/')
760 path += '_temp_view';
761 path += $.jqcouch.generate_query_str(query_args);
763 var server_url = $.jqcouch.server_url;
765 if (typeof(map) == 'string') {
769 if ($.browser.safari) {
772 var map = map.toSource();
776 url: server_url + path,
782 contentType: 'application/json',
784 contentType: 'application/javascript',
786 error: function(request){
787 $.jqcouch.view.on_error(request, {
788 method: 'jqcouch.doc.temp',
789 action: '_temp_view',
790 args: {query_args: query_args}
794 success: function(data){
795 $.jqcouch.view.on_success(data, {
796 method: 'jqcouch.doc.temp',
797 action: '_temp_view',
798 args: {query_args: query_args}
805 on_error: function(request, caller)
807 $.jqcouch.on_error(request, caller);
809 on_success: function(data, caller)
811 $.jqcouch.on_success(data, caller);
813 _original_on_error: this.on_error,
814 _original_on_success: this.on_success