Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update vendor couchapp

  • Loading branch information...
commit 3b63dfa3d80b5e95d2d0c768c03231c854c27457 1 parent 6d012f0
@jchris jchris authored
View
9 _attachments/index.html
@@ -6,5 +6,14 @@
</head>
<body>
<p>Visit /pages/recent</p>
+ <script src="script/myloader.js"></script>
+ <script src="/_utils/script/base64.js"></script>
+ <script src="/_utils/script/jquery.form.js"></script>
+ <script type="text/javascript" charset="utf-8">
+ $.couch.db("pages").info();
+ $(function() {
+ $.couch.db("a").info();
+ });
+ </script>
</body>
</html>
View
333 vendor/couchapp/_attachments/jquery.couch.app.js
@@ -19,7 +19,7 @@
// });
(function($) {
-
+
function Design(db, name) {
this.doc_id = "_design/"+name;
this.view = function(view, opts) {
@@ -30,229 +30,122 @@
};
}
- $.couch.app = $.couch.app || function(appFun, opts) {
- opts = opts || {};
- $(function() {
- var urlPrefix = opts.urlPrefix || "";
- $.couch.urlPrefix = urlPrefix;
-
- var index = urlPrefix.split('/').length;
- var fragments = unescape(document.location.href).split('/');
- var dbname = opts.db || fragments[index + 2];
- var dname = opts.design || fragments[index + 4];
-
- var db = $.couch.db(dbname);
- var design = new Design(db, dname);
-
- // docForm applies CouchDB behavior to HTML forms.
- // todo make this a couch.app plugin
- function docForm(formSelector, opts) {
- var localFormDoc = {};
- opts = opts || {};
- opts.fields = opts.fields || [];
+ function docForm() { alert("docForm has been moved to vendor/couchapp/lib/docForm.js, use app.require to load") };
- // turn the form into deep json
- // field names like 'author-email' get turned into json like
- // {"author":{"email":"quentin@example.com"}}
- function formToDeepJSON(form, fields, doc) {
- form = $(form);
- fields.forEach(function(field) {
- var element = form.find("[name="+field+"]");
- if (element.attr('type') === 'checkbox') {
- var val = element.attr('checked');
- } else {
- var val = element.val();
- if (!val) return;
- }
- var parts = field.split('-');
- var frontObj = doc, frontName = parts.shift();
- while (parts.length > 0) {
- frontObj[frontName] = frontObj[frontName] || {};
- frontObj = frontObj[frontName];
- frontName = parts.shift();
- }
- frontObj[frontName] = val;
- });
- }
-
- // Apply the behavior
- $(formSelector).submit(function(e) {
- e.preventDefault();
- if (opts.validate && opts.validate() == false) { return false;}
- // formToDeepJSON acts on localFormDoc by reference
- formToDeepJSON(this, opts.fields, localFormDoc);
- if (opts.beforeSave) {opts.beforeSave(localFormDoc);}
- db.saveDoc(localFormDoc, {
- success : function(resp) {
- if (opts.success) {opts.success(resp, localFormDoc);}
- }
- });
-
- return false;
- });
+ function resolveModule(names, parent, current) {
+ if (names.length === 0) {
+ if (typeof current != "string") {
+ throw ["error","invalid_require_path",
+ 'Must require a JavaScript string, not: '+(typeof current)];
+ }
+ return [current, parent];
+ }
+ var n = names.shift();
+ if (n == '..') {
+ if (!(parent && parent.parent)) {
+ throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
+ }
+ return resolveModule(names, parent.parent.parent, parent.parent);
+ } else if (n == '.') {
+ if (!parent) {
+ throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
+ }
+ return resolveModule(names, parent.parent, parent);
+ }
+ if (!current[n]) {
+ throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
+ }
+ var p = current;
+ current = current[n];
+ current.parent = p;
+ return resolveModule(names, p, current);
+ }
- // populate form from an existing doc
- function docToForm(doc) {
- var form = $(formSelector);
- // fills in forms
- opts.fields.forEach(function(field) {
- var parts = field.split('-');
- var run = true, frontObj = doc, frontName = parts.shift();
- while (frontObj && parts.length > 0) {
- frontObj = frontObj[frontName];
- frontName = parts.shift();
- }
- if (frontObj && frontObj[frontName]) {
- var element = form.find("[name="+field+"]");
- if (element.attr('type') === 'checkbox') {
- element.attr('checked', frontObj[frontName]);
- } else {
- element.val(frontObj[frontName]);
- }
- }
- });
+ function makeRequire(ddoc) {
+ var moduleCache = [];
+ function getCachedModule(name, parent) {
+ var key, i, len = moduleCache.length;
+ for (i=0;i<len;++i) {
+ key = moduleCache[i].key;
+ if (key[0] === name && key[1] === parent) {
+ return moduleCache[i].module;
}
-
- if (opts.id) {
- db.openDoc(opts.id, {
- attachPrevRev : opts.attachPrevRev,
- error: function() {
- if (opts.error) {opts.error.apply(opts, arguments);}
- },
- success: function(doc) {
- if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
- localFormDoc = doc;
- docToForm(doc);
- }});
- } else if (opts.template) {
- if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
- localFormDoc = opts.template;
- docToForm(localFormDoc);
- }
- var instance = {
- deleteDoc : function(opts) {
- opts = opts || {};
- if (confirm("Really delete this document?")) {
- db.removeDoc(localFormDoc, opts);
- }
- },
- localDoc : function() {
- formToDeepJSON(formSelector, opts.fields, localFormDoc);
- return localFormDoc;
- }
- };
- return instance;
}
-
- function resolveModule(names, parent, current) {
- if (names.length === 0) {
- if (typeof current != "string") {
- throw ["error","invalid_require_path",
- 'Must require a JavaScript string, not: '+(typeof current)];
- }
- return [current, parent];
- }
- // we need to traverse the path
- var n = names.shift();
- if (n == '..') {
- if (!(parent && parent.parent)) {
- throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
- }
- return resolveModule(names, parent.parent.parent, parent.parent);
- } else if (n == '.') {
- if (!parent) {
- throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
- }
- return resolveModule(names, parent.parent, parent);
- }
- if (!current[n]) {
- throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
- }
- var p = current;
- current = current[n];
- current.parent = p;
- return resolveModule(names, p, current);
+ return null;
+ }
+ function setCachedModule(name, parent, module) {
+ moduleCache.push({ key: [name, parent], module: module });
+ }
+ var require = function (name, parent) {
+ var cachedModule = getCachedModule(name, parent);
+ if (cachedModule !== null) {
+ return cachedModule;
}
-
- var p = document.location.pathname.split('/');
- p.shift();
- var qs = document.location.search.replace(/^\?/,'').split('&');
- var q = {};
- qs.forEach(function(param) {
- var ps = param.split('=');
- var k = decodeURIComponent(ps[0]);
- var v = decodeURIComponent(ps[1]);
- if (["startkey", "endkey", "key"].indexOf(k) != -1) {
- q[k] = JSON.parse(v);
- } else {
- q[k] = v;
- }
- });
- var mockReq = {
- path : p,
- query : q
- };
-
- var appExports = $.extend({
- db : db,
- design : design,
- view : design.view,
- list : design.list,
- docForm : docForm,
- req : mockReq
- }, $.couch.app.app);
+ var exports = {};
+ var resolved = resolveModule(name.split('/'), parent, ddoc);
+ var source = resolved[0];
+ parent = resolved[1];
+ var s = "var func = function (exports, require) { " + source + " };";
+ try {
+ eval(s);
+ func.apply(ddoc, [exports, function(name) {return require(name, parent, source)}]);
+ } catch(e) {
+ throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
+ }
+ setCachedModule(name, parent, exports);
+ return exports;
+ }
+ return require;
+ };
- function handleDDoc(ddoc) {
- var moduleCache = [];
-
- function getCachedModule(name, parent) {
- var key, i, len = moduleCache.length;
- for (i=0;i<len;++i) {
- key = moduleCache[i].key;
- if (key[0] === name && key[1] === parent) {
- return moduleCache[i].module;
- }
- }
-
- return null;
- }
-
- function setCachedModule(name, parent, module) {
- moduleCache.push({ key: [name, parent], module: module });
- }
-
- if (ddoc) {
- var require = function(name, parent) {
- var cachedModule = getCachedModule(name, parent);
- if (cachedModule !== null) {
- return cachedModule;
- }
-
- var exports = {};
- var resolved = resolveModule(name.split('/'), parent, ddoc);
- var source = resolved[0];
- parent = resolved[1];
- var s = "var func = function (exports, require) { " + source + " };";
- try {
- eval(s);
- func.apply(ddoc, [exports, function(name) {return require(name, parent, source)}]);
- } catch(e) {
- throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
- }
-
- setCachedModule(name, parent, exports);
-
- return exports;
- }
- appExports.ddoc = ddoc;
- appExports.require = require;
- }
- // todo make app-exports the this in the execution context?
- appFun.apply(appExports, [appExports]);
+ function mockReq() {
+ var p = document.location.pathname.split('/'),
+ qs = document.location.search.replace(/^\?/,'').split('&'),
+ q = {};
+ qs.forEach(function(param) {
+ var ps = param.split('='),
+ k = decodeURIComponent(ps[0]),
+ v = decodeURIComponent(ps[1]);
+ if (["startkey", "endkey", "key"].indexOf(k) != -1) {
+ q[k] = JSON.parse(v);
+ } else {
+ q[k] = v;
}
-
+ });
+ p.shift();
+ return {
+ path : p,
+ query : q
+ };
+ };
+
+ $.couch.app = $.couch.app || function(appFun, opts) {
+ opts = opts || {};
+ var urlPrefix = (opts.urlPrefix || ""),
+ index = urlPrefix.split('/').length,
+ fragments = unescape(document.location.href).split('/'),
+ dbname = opts.db || fragments[index + 2],
+ dname = opts.design || fragments[index + 4];
+ $.couch.urlPrefix = urlPrefix;
+ var db = $.couch.db(dbname),
+ design = new Design(db, dname);
+ var appExports = $.extend({
+ db : db,
+ design : design,
+ view : design.view,
+ list : design.list,
+ docForm : docForm, // deprecated
+ req : mockReq()
+ }, $.couch.app.app);
+ function handleDDoc(ddoc) {
+ if (ddoc) {
+ appExports.ddoc = ddoc;
+ appExports.require = makeRequire(ddoc);
+ }
+ appFun.apply(appExports, [appExports]);
+ }
if ($.couch.app.ddocs[design.doc_id]) {
- handleDDoc($.couch.app.ddocs[design.doc_id])
+ $(function() {handleDDoc($.couch.app.ddocs[design.doc_id])});
} else {
// only open 1 connection for this ddoc
if ($.couch.app.ddoc_handlers[design.doc_id]) {
@@ -264,22 +157,20 @@
success : function(doc) {
$.couch.app.ddocs[design.doc_id] = doc;
$.couch.app.ddoc_handlers[design.doc_id].forEach(function(h) {
- h(doc);
+ $(function() {h(doc)});
});
$.couch.app.ddoc_handlers[design.doc_id] = null;
},
error : function() {
$.couch.app.ddoc_handlers[design.doc_id].forEach(function(h) {
- h();
+ $(function() {h()});
});
$.couch.app.ddoc_handlers[design.doc_id] = null;
}
});
}
}
-
- });
- };
+ };
$.couch.app.ddocs = {};
$.couch.app.ddoc_handlers = {};
// legacy support. $.CouchApp is deprecated, please use $.couch.app
View
126 vendor/couchapp/lib/docform.js
@@ -14,48 +14,49 @@
// field names like 'author-email' get turned into json like
// {"author":{"email":"quentin@example.com"}}
// acts on doc by reference, so you can safely pass non-form fields through
-function formToDeepJSON(form, fields, doc) {
- form = $(form);
- fields.forEach(function(field) {
- var val = form.find("[name="+field+"]").val();
- if (!val) {return;}
- var parts = field.split('-');
- var frontObj = doc, frontName = parts.shift();
- while (parts.length > 0) {
- frontObj[frontName] = frontObj[frontName] || {};
- frontObj = frontObj[frontName];
- frontName = parts.shift();
- }
- frontObj[frontName] = val;
- });
-}
-
-function onSubmit(form, db, doc, opts) {
- formToDeepJSON(form, opts.fields, doc);
- if (opts.beforeSave) {opts.beforeSave(doc);}
- db.saveDoc(localFormDoc, {
- success : function(resp) {
- if (opts.success) {opts.success(resp, doc);}
- }
- });
-};
-
-function applyFields(form, doc) {
-
-};
-exports.applyFields = applyFields;
-// docForm applies CouchDB behavior to HTML forms.
-// todo make this a couch.app plugin
function docForm(formSelector, opts) {
var localFormDoc = {};
opts = opts || {};
opts.fields = opts.fields || [];
+ // turn the form into deep json
+ // field names like 'author-email' get turned into json like
+ // {"author":{"email":"quentin@example.com"}}
+ function formToDeepJSON(form, fields, doc) {
+ form = $(form);
+ fields.forEach(function(field) {
+ var element = form.find("[name="+field+"]");
+ if (element.attr('type') === 'checkbox') {
+ var val = element.attr('checked');
+ } else {
+ var val = element.val();
+ if (!val) return;
+ }
+ var parts = field.split('-');
+ var frontObj = doc, frontName = parts.shift();
+ while (parts.length > 0) {
+ frontObj[frontName] = frontObj[frontName] || {};
+ frontObj = frontObj[frontName];
+ frontName = parts.shift();
+ }
+ frontObj[frontName] = val;
+ });
+ }
+
// Apply the behavior
$(formSelector).submit(function(e) {
-
-
+ e.preventDefault();
+ if (opts.validate && opts.validate() == false) { return false;}
+ // formToDeepJSON acts on localFormDoc by reference
+ formToDeepJSON(this, opts.fields, localFormDoc);
+ if (opts.beforeSave) {opts.beforeSave(localFormDoc);}
+ db.saveDoc(localFormDoc, {
+ success : function(resp) {
+ if (opts.success) {opts.success(resp, localFormDoc);}
+ }
+ });
+
return false;
});
@@ -71,38 +72,43 @@ function docForm(formSelector, opts) {
frontName = parts.shift();
}
if (frontObj && frontObj[frontName]) {
- form.find("[name="+field+"]").val(frontObj[frontName]);
+ var element = form.find("[name="+field+"]");
+ if (element.attr('type') === 'checkbox') {
+ element.attr('checked', frontObj[frontName]);
+ } else {
+ element.val(frontObj[frontName]);
+ }
}
- });
+ });
}
-
+
if (opts.id) {
db.openDoc(opts.id, {
+ attachPrevRev : opts.attachPrevRev,
+ error: function() {
+ if (opts.error) {opts.error.apply(opts, arguments);}
+ },
success: function(doc) {
- if (opts.onLoad) {opts.onLoad(doc);}
+ if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
localFormDoc = doc;
docToForm(doc);
- }});
- } else if (opts.template) {
- if (opts.onLoad) {opts.onLoad(opts.template);}
- localFormDoc = opts.template;
- docToForm(localFormDoc);
+ }});
+ } else if (opts.template) {
+ if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
+ localFormDoc = opts.template;
+ docToForm(localFormDoc);
+ }
+ var instance = {
+ deleteDoc : function(opts) {
+ opts = opts || {};
+ if (confirm("Really delete this document?")) {
+ db.removeDoc(localFormDoc, opts);
}
- var instance = {
- deleteDoc : function(opts) {
- opts = opts || {};
- if (confirm("Really delete this document?")) {
- db.removeDoc(localFormDoc, opts);
- }
- },
- localDoc : function() {
- formToDeepJSON(formSelector, opts.fields, localFormDoc);
- return localFormDoc;
- }
- };
- return instance;
+ },
+ localDoc : function() {
+ formToDeepJSON(formSelector, opts.fields, localFormDoc);
+ return localFormDoc;
}
-
-
-
-
+ };
+ return instance;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.