Created
January 16, 2013 00:11
-
-
Save simon-jouet/4543421 to your computer and use it in GitHub Desktop.
Cometd factory for angularjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
factory('cometd', function($rootScope) { | |
var cometd = $.cometd; | |
// Configure cometd | |
cometd.configure({ | |
url: location.protocol + '//' + location.host + config.contextPath + '/cometd', | |
logLevel: 'info' | |
}); | |
// Add a listener for the handshake *TODO* what should be done if message fails ? | |
cometd.addListener('/meta/handshake', function(message) { console.log(message) }); | |
// Handshake with the server | |
cometd.handshake(); | |
return { | |
addListener: function(channel, scope, callback) { | |
cometd.addListener(channel, scope, function() { | |
var args = arguments; | |
$rootScope.$apply(function() { | |
callback.apply(cometd, args); | |
}) | |
}); | |
}, | |
removeListener: function(subscription) { | |
cometd.removeListener(subscription); | |
}, | |
subscribe: function(channel, scope, callback, subscribeProps) { | |
cometd.subscribe(channel, scope, function() { | |
var args = arguments; | |
$rootScope.$apply(function() { | |
callback.apply(cometd, args); | |
}) | |
}, subscribeProps); | |
}, | |
unsubscribe: function(subscription, unsubscribeProps) { | |
cometd.unsubscribe(subscription, unsubscribeProps); | |
}, | |
publish: function(channel, content, publishProps, publishCallback) { | |
cometd.publish(channel, content, publishProps, function() { | |
var args = arguments; | |
$rootScope.$apply(function() { | |
publishCallback.apply(cometd, args); | |
}) | |
}); | |
}, | |
disconnect: function(sync, disconnectProps) { | |
cometd.disconnect(sync, disconnectProps); | |
}, | |
batch: function(scope, callback) { | |
cometd.batch(scope, function() { | |
var args = arguments; | |
$rootScope.$apply(function() { | |
callback.apply(cometd, args); | |
}) | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment