Skip to content

Instantly share code, notes, and snippets.

@rogerdudler
Created August 19, 2011 09:35
Show Gist options
  • Save rogerdudler/1156456 to your computer and use it in GitHub Desktop.
Save rogerdudler/1156456 to your computer and use it in GitHub Desktop.
Terrific Socket connector
(function($) {
Tc.Connector.Socket = Tc.Connector.extend({
events: [],
init: function(connectorId) {
this._super(connectorId);
var that = this;
this.events = $('body').data('connector-socket').split(',');
for (var i = 0, len = this.events.length; i < len; i++) {
var event = this.events[i];
socket.on(event, function(data) {
var eventInternal = event.substr(0,1).toUpperCase() + event.substr(1);
that._notify('socket', 'on' + eventInternal, data, function() { });
})
}
},
notify: function(component, state, data, callback) {
var event = state.substr(2,1).toLowerCase() + state.substr(3);
if (jQuery.inArray(event, this.events) > 0) {
socket.emit(event, data, callback);
}
return this._notify(component, state, data, callback);
},
_notify: function(component, state, data, callback) {
var proceed = true,
components = this.components;
for (var id in components) {
if (components[id].component !== component && components[id].component[state]) {
if (components[id].component[state](data, callback) === false) {
proceed = false;
}
}
}
return proceed;
}
});
})(Tc.$);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment