Skip to content

Instantly share code, notes, and snippets.

@juanmf
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanmf/10475007 to your computer and use it in GitHub Desktop.
Save juanmf/10475007 to your computer and use it in GitHub Desktop.
/*
* I needed it 'cause I have a generic js to add embeded subForms to a Form
* appending the prototype upon AddButton JQuery click event. So to add event
* bindings to one of the controls generated out of the prototype template, which
* is unknown to the js that adds it, I fire a message like "prototype_just_added"
* and the specific js catches it and binds the event.
*/
/*
* MediatorPatter implementaton to allow for some behavior extensions.
*
*
* Example use:
* <code>
* m = new docdigital.Mediator(); // Central Mediator Object.
* a = new docdigital.Subscriber('name', function(message, from) {alert(message)}); // `a` is a handler
* m.register(a, 'allowAddFields.crearNuevo'); // `a` listens for 'allowAddFields.crearNuevo' message and alerts it
* m.send('allowAddFields.crearNuevo'); // Mediator notifies a, sender is undefined (i.e. anonymous).
*
* // pair [message, subscriber.name] must be unique.
* b = new docdigital.Subscriber('name2', function(message, from) {alert(message)});
* c = new docdigital.Subscriber('name3', function(message, from) {alert(message)});
* m.register(b, 'allowAddFields.crearNuevo'); // `a` listens for 'allowAddFields.crearNuevo' message and alerts it
* m.send('allowAddFields.crearNuevo', b); // Mediator notifies only (`a` and `c`) as `b` is sender.
* m.send('allowAddFields.crearNuevo', b, a); // Mediator notifies only `a` as b is sending to a.
* m.send('allowAddFields.crearNuevo', a, a); // Mediator notifies only `a` message from `a`.
* // Only listeners MUST be registered, sender could be any object, or none.
* </code>
*
* Inspired in {@link http://www.dofactory.com/javascript-mediator-pattern.aspx} chat sample code
* added the possibility to notify only the listeners registered to the sending message.
*/
var docdigital = docdigital || {};
$.extend(true, docdigital, {
constants: {
mediator: {
messages: {}
}
},
mediatorInstance: null,
Subscriber: function(name, receive) {
this.name = name;
this.Mediator = null;
this.receive = receive || this.receive;
},
Mediator: function() {
var subscribers = {};
return {
register: function(subscriber, message) {
message = message || 'empty';
subscribers[message] = subscribers[message] || {};
subscribers[message][subscriber.name] = subscriber;
subscriber.Mediator = this;
},
send: function(message, from, to) {
if (to) { // single message
to.receive(message, from);
} else { // broadcast message to message's subscribers
for (key in subscribers[message]) {
if (subscribers[message][key] !== from) {
if (false === subscribers[message][key].receive(message, from)) {
break;
}
}
}
}
}
};
}
});
docdigital.Subscriber.prototype = {
send: function(message, to) {
this.Mediator.send(message, this, to);
},
receive: function(message, from) {}
};
docdigital.mediatorInstance = new docdigital.Mediator();
/**
* Example usage follows
*/
/**
* Adding message in the sender file (in my case the js adding prototype subForms),
* this doesn't need to be a constant, you could spread repeated literal strings too..
* And sending the message when appropriate.
*/
var docdigital = docdigital || {};
$.extend(true, docdigital, {
constants: {
mediator: {
messages: {
clonePrototype_prototypeAdded: 'clonePrototype_prototypeAdded'
}
}
},
ColonePrototype: function () {
// ... generic code to append prototype templates
$clone = $.parseHTML($clone);
$(this).append($clone);
docdigital.mediatorInstance.send(
docdigital.constants.mediator.messages.clonePrototype_prototypeAdded,
$clone
);
}
});
/**
* registering a listener/handler somewhere else.
*/
// subscriber is binded to a callback fn
var subscriber = new docdigital.Subscriber('buttonAdd', docdigital.Listeners.documentType.fieldTypeChangeEvent);
// register subscriber to a message. docdigital.constants.mediator.messages.clonePrototype_prototypeAdded
docdigital.mediatorInstance.register(
subscriber, docdigital.constants.mediator.messages.clonePrototype_prototypeAdded
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment