Skip to content

Commit

Permalink
simplify evently widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Apr 28, 2010
1 parent abcefba commit d1ed369
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
21 changes: 8 additions & 13 deletions _attachments/script/app.js
@@ -1,26 +1,21 @@
// $.couch.app() loads the design document from the server and
// then calls our application.
$.couch.app(function(app) {
// console.log(app.ddoc);

$.couch.app(function(app) {
// An evently widget that displays a tag cloud which is updated when
// the underlying data changes. The code for this widget is stored
// in the evently/tagcloud directory.
$("#tagcloud").evently(app.ddoc.evently.tagcloud, app);
$("#tagcloud").evently("tagcloud", app);

// this is the same thing, but for a cloud of usernames
$("#usercloud").evently(app.ddoc.evently.usercloud, app);

// customize the couchapp profile widget with our templates and selectors
$.extend(true,
app.ddoc.vendor.couchapp.evently.profile,
app.ddoc.evently.profile);
$("#usercloud").evently("usercloud", app);

// apply the widget to the dom
$("#profile").evently(app.ddoc.vendor.couchapp.evently.profile, app);
// we customize the profile widget in our evently directory but also
// we use code from vendor/couchapp/evently
// evently knows about this and it just works.
$("#profile").evently("profile", app);

// setup the account widget
$("#account").evently(app.ddoc.vendor.couchapp.evently.account, app);
$("#account").evently("account", app);

// trigger the profile widget's events corresponding to the account widget
$.evently.connect($("#account"), $("#profile"), ["loggedIn", "loggedOut"]);
Expand Down
26 changes: 24 additions & 2 deletions vendor/couchapp/_attachments/jquery.evently.js
Expand Up @@ -53,13 +53,13 @@ function $$(node) {
$.evently = {
connect : function(source, target, events) {
events.forEach(function(ev) {
source.bind(ev, function() {
$(source).bind(ev, function() {
var args = $.makeArray(arguments);
// remove the original event to keep from stacking args extra deep
// it would be nice if jquery had a way to pass the original
// event to the trigger method.
args.shift();
target.trigger(ev, args);
$(target).trigger(ev, args);
return false;
});
});
Expand All @@ -68,12 +68,34 @@ function $$(node) {
changesDBs : {}
};

function extractFrom(name, evs) {
return evs[name];
};

function extractEvents(name, ddoc) {
// extract events from ddoc.evently and ddoc.vendor.*.evently
var events = [true, {}];
$.forIn(ddoc.vendor, function(k, v) {
if (v.evently && v.evently[name]) {
events.push(v.evently[name]);
}
});
if (ddoc.evently[name]) {events.push(ddoc.evently[name]);}
return $.extend.apply(null, events);
}

$.fn.evently = function(events, app, args) {
var elem = $(this);
// store the app on the element for later use
if (app) {
$$(elem).app = app;
}

if (typeof events == "string") {
events = extractEvents(events, app.ddoc);
$.log(events)
}

$$(elem).evently = events;
// setup the handlers onto elem
forIn(events, function(name, h) {
Expand Down

0 comments on commit d1ed369

Please sign in to comment.