+ // OLD PLUGIN API: extend jQuery/$ with pubsub capabilities
+ // https://gist.github.com/661855
+ var o = $({});
+ $.subscribe = function( channel, selector, data, fn) {
+ /* borrowed from jQuery */
+ if ( data == null && fn == null ) {
+ // ( channel, fn )
+ fn = selector;
+ data = selector = undefined;
+ } else if ( fn == null ) {
+ if ( typeof selector === "string" ) {
+ // ( channel, selector, fn )
+ fn = data;
+ data = undefined;
+ } else {
+ // ( channel, data, fn )
+ fn = data;
+ data = selector;
+ selector = undefined;
+ }
+ }
+ /* </ugly> */
+
+ /* We use an indirection function that will clone the object passed in
+ * parameter to the subscribe callback
+ *
+ * FIXME currently we only clone query objects which are the only ones
+ * supported and editable, we might have the same issue with results but
+ * the page load time will be severely affected...
+ */
+ o.on.apply(o, [channel, selector, data, function() {
+ for(i = 1; i < arguments.length; i++) {
+ if ( arguments[i].constructor.name == 'ManifoldQuery' )
+ arguments[i] = arguments[i].clone();
+ }
+ fn.apply(o, arguments);
+ }]);
+ };
+
+ $.unsubscribe = function() {
+ o.off.apply(o, arguments);
+ };
+
+ $.publish = function() {
+ o.trigger.apply(o, arguments);
+ };
+