Question about Setting Hooks

3 views
Skip to first unread message

Philip Weaver

unread,
Apr 15, 2009, 4:47:23 AM4/15/09
to dovetaildb-discuss
When a user applies custom hooks, do they wrap the existing hooks defined in core.js or are they replaced? If they do get replaced, what do we lose? Would it be possible idea to have them wrap somehow instead?

psch...@gmail.com

unread,
Apr 16, 2009, 12:43:32 AM4/16/09
to dovetaildb-discuss
This is a question that I struggled with a bit: currently, when you
call setXXXHook() it *replaces* any existing hooks (but only in that
database -- so you don't have to worry about code in your
application's database doing bad things to core.js in _metadata).

Wrapping would be a more natural behavior, but it can get into an area
where developer preferences can diverge, so, at the moment, I'm
leaving that up to the developer. You can build your own wrapping
behavior like so (together, the two handlers give every result a foo
property equal to 10):


function queryHandler1(innerfn, bag, query, options) {
var results = innerfn(bag, query, options);
for (var i = 0; i < results.length; i++) {
results[i].foo = 5;
}
return results;
}

function queryHandler2(innerfn, bag, query, options) {
var results = innerfn(bag, query, options);
for (var i = 0; i < results.length; i++) {
results[i].foo += 5;
}
return results;
}

queryHandlers = [queryHandler1,queryHandler2];

function queryHookAt(i,bag, query, options) {
var innerfn;
if (i==0) innerfn = function(b,q,o){return _server.inner.query
(b,q,o);}
else innerfn = function(b,q,o){return queryHookAt(i-1,b,q,o);}
return queryHandlers[i](innerfn, bag, query, options);
}

_server.setQueryHook(function(b,q,o){return queryHookAt
(queryHandlers.length-1,b,q,o);});



There's some advantage to standardizing on a mechanism like this,
though, so that people could write "plugins" and share them with
others. A plugin for giving you computed (but not stored) values, a
plugin for writing all updates to an "audit" bag, etc.
Reply all
Reply to author
Forward
0 new messages