Skip to content

Commit

Permalink
latest couchapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Apr 3, 2010
1 parent 3348d04 commit c50e023
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 18 deletions.
13 changes: 11 additions & 2 deletions vendor/couchapp/_attachments/jquery.couch.app.util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$.log = function() {
$.log = function(m) {
if (window && window.console && window.console.log) {
window.console.log(arguments);
window.console.log(arguments.length == 1 ? m : arguments);
}
};

Expand Down Expand Up @@ -55,3 +55,12 @@ $.prettyDate = function(time){
day_diff < 730 && Math.ceil( day_diff / 31 ) + " months ago" ||
Math.ceil( day_diff / 365 ) + " years ago";
};

$.argsToArray = function(args) {
if (!args.callee) return args;
var array = [];
for (var i=0; i < args.length; i++) {
array.push(args[i]);
};
return array;
}
20 changes: 11 additions & 9 deletions vendor/couchapp/_attachments/jquery.evently.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function $$(node) {
if (app) {
$$(elem).app = app;
}

$$(elem).evently = events;
// setup the handlers onto elem
forIn(events, function(name, h) {
eventlyHandler(elem, name, h, args);
Expand Down Expand Up @@ -105,7 +105,7 @@ function $$(node) {
}
var f = funViaString(h);
if (typeof f == "function") {
elem.bind(name, {args:args}, f);
elem.bind(name, {args:args}, f);
} else if (typeof f == "string") {
elem.bind(name, {args:args}, function() {
$(this).trigger(f, arguments);
Expand All @@ -131,6 +131,7 @@ function $$(node) {
};

$.fn.replace = function(elem) {
$.log("Replace", this)
$(this).empty().append(elem);
};

Expand All @@ -146,7 +147,6 @@ function $$(node) {
// $.log("query before renderElement", arguments)
runQuery(me, h, args)
} else if (h.async && !qrun) {
$.log("runAsync")
runAsync(me, h, args)
} else {
// $.log("renderElement")
Expand All @@ -156,6 +156,7 @@ function $$(node) {
var act = h.render || "replace";
var app = $$(me).app;
if (h.mustache) {
$.log("rendering", h.mustache)
var newElem = mustachioed(me, h, args);
me[act](newElem);
}
Expand Down Expand Up @@ -186,12 +187,12 @@ function $$(node) {
runIfFun(me, h.partials, args)));
};

function runAsync(me, h, args) {
var app = $$(me).app;
function runAsync(me, h, args) {
// the callback is the first argument
funViaString(h.async).apply(me, [function() {
renderElement(me, h, arguments, true);
}].concat(args));
renderElement(me, h,
$.argsToArray(arguments).concat($.argsToArray(args)), true);
}].concat($.argsToArray(args)));
};


Expand All @@ -213,17 +214,18 @@ function $$(node) {
q.success = function(resp) {
$.log("runQuery newRows success", resp.rows.length, me, resp)
resp.rows.reverse().forEach(function(row) {
renderElement(me, h, [row], true)
renderElement(me, h, [row].concat($.argsToArray(args)), true)
});
if (userSuccess) userSuccess(resp);
};
newRows(me, app, viewName, q);
} else {
q.success = function(resp) {
// $.log("runQuery success", resp)
renderElement(me, h, [resp], true);
renderElement(me, h, [resp].concat($.argsToArray(args)), true);
userSuccess && userSuccess(resp);
};
$.log(app)
app.view(viewName, q);
}
}
Expand Down
25 changes: 19 additions & 6 deletions vendor/couchapp/_attachments/jquery.pathbinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
var PATH_REPLACER = "([^\/]+)",
PATH_NAME_MATCHER = /:([\w\d]+)/g,
QUERY_STRING_MATCHER = /\?([^#]*)$/,
SPLAT_MATCHER = /(\*)/,
SPLAT_REPLACER = "(.+)",
_currentPath,
_lastPath,
_pathInterval;
Expand Down Expand Up @@ -33,7 +35,7 @@
})
},
go : function(path) {
goPath(path);
goPath(path);
triggerOnPath(path);
},
onChange : function (fun) {
Expand All @@ -60,17 +62,26 @@

function triggerOnPath(path) {
$.pathbinder.changeFuns.forEach(function(fun) {fun(path)});
var pathSpec, path_params, params = {};
var pathSpec, path_params, params = {}, param_name, param;
for (var i=0; i < $.pathbinder.paths.length; i++) {
pathSpec = $.pathbinder.paths[i];
$.log("pathSpec", pathSpec);
if ((path_params = pathSpec.matcher.exec(path)) !== null) {
$.log("path_params", path_params);
path_params.shift();
for (var j=0; j < path_params.length; j++) {
params[pathSpec.param_names[j]] = decodeURIComponent(path_params[j]);
param_name = pathSpec.param_names[j];
param = decodeURIComponent(path_params[j]);
if (param_name) {
params[param_name] = param;
} else {
if (!params.splat) params.splat = [];
params.splat.push(param);
}
};
// $.log("path trigger for "+path);
pathSpec.callback(params);
return true;
// return true; // removed this to allow for multi match
}
};
};
Expand Down Expand Up @@ -117,10 +128,12 @@

return {
param_names : param_names,
matcher : new RegExp(path.replace(PATH_NAME_MATCHER, PATH_REPLACER) + "$"),
matcher : new RegExp(path.replace(
PATH_NAME_MATCHER, PATH_REPLACER).replace(
SPLAT_MATCHER, SPLAT_REPLACER) + "$"),
template : path.replace(PATH_NAME_MATCHER, function(a, b) {
return '{{'+b+'}}';
}),
}).replace(SPLAT_MATCHER, '{{splat}}'),
callback : callback
};
};
Expand Down
3 changes: 2 additions & 1 deletion vendor/couchapp/evently/account/loggedIn/after.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// todo move to template
function(e, r) {
$(this).attr("data-name", r.userCtx.name);
}
$$(this).userCtx = r.userCtx;
}
25 changes: 25 additions & 0 deletions vendor/couchapp/lib/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exports.get = function(db, docid, setFun, getFun) {
db.openDoc(docid, {
success : function(doc) {
getFun(doc.cache);
},
error : function() {
setFun(function(cache) {
db.saveDoc({
_id : docid,
cache : cache
});
getFun(cache);
});
}
});
};

exports.clear = function(db, docid) {
db.openDoc(docid, {
success : function(doc) {
db.removeDoc(doc);
},
error : function() {}
});
};

0 comments on commit c50e023

Please sign in to comment.