-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoux.js
122 lines (114 loc) · 3.36 KB
/
coux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// coux is a tiny couch client, there are implementations for server side and client side
// this implementation is for Zepto or jQuery.
function coux(opts, body) {
var query = [], k, v, q, cb = arguments[arguments.length-1]
, req = {
type: 'GET',
dataType: 'json',
contentType: 'application/json',
success: function(doc) {
cb(false, doc)
},
error: function(e) {
console.log(e.responseText || e)
console.log(opts.url)
cb(e)
}
};
if (typeof opts === 'string' || $.isArray(opts)) {
opts = {url:opts};
}
if (arguments.length == 3) {
opts.data = JSON.stringify(body);
}
opts.url = opts.url || opts.uri;
delete opts.uri;
if ($.isArray(opts.url)) {
if (typeof opts.url[opts.url.length-1] == 'object') {
q = opts.url[opts.url.length-1];
opts.url = opts.url.slice(0, opts.url.length-1);
for (k in q) {
if (['startkey', 'endkey', 'key'].indexOf(k) !== -1) {
v = JSON.stringify(q[k])
} else {
v = q[k];
}
query.push(encodeURIComponent(k)+'='+encodeURIComponent(v));
}
query = query.join('&');
}
var first = true;
opts.url = (opts.url.map(function(path) {
if (first) {
first = false;
if (/^http/.test(path)) {
//if (/\/$/.test(path)) {
// return path.substring(0,path.length-1)
//} else {
return path;
//}
}
}
return encodeURIComponent(path);
})).join('/');
if (query) {
opts.url = opts.url + "?" + query;
}
}
for (var x in opts) {
if (opts.hasOwnProperty(x)){
req[x] = opts[x];
}
}
// console.log("req", req)
$.ajax(req);
};
coux.put = function() {
var opts = arguments[0];
if (typeof opts === 'string' || Array.isArray(opts)) {
opts = {url:opts};
}
opts.type = "PUT";
arguments[0] = opts;
coux.apply(this, arguments);
};
coux.post = function() {
var opts = arguments[0];
if (typeof opts === 'string' || Array.isArray(opts)) {
opts = {url:opts};
}
opts.type = "POST";
arguments[0] = opts;
coux.apply(this, arguments);
};
coux.del = function() {
var opts = arguments[0];
if (typeof opts === 'string' || Array.isArray(opts)) {
opts = {url:opts};
}
opts.type = "DELETE";
arguments[0] = opts;
coux.apply(this, arguments);
};
coux.get = coux;
coux.changes = function(dbname, onDBChange) {
var since = 0;
function changesCallback(opts) {
since = opts.last_seq || since;
if (opts.results) {onDBChange(opts);}
coux([dbname, '_changes', {feed : 'longpoll', since : since}], function(err, data) {
if (!err) {
changesCallback(data)
} else {
setTimeout(function() {
if (console && console.log) {
console.log("error changes", err);
console.log(opts);
}
changesCallback({last_seq : since});
}, 2500)
}
});
}
changesCallback({last_seq : 0});
};