Skip to content

Instantly share code, notes, and snippets.

@leon
Last active December 14, 2015 11:49
Show Gist options
  • Save leon/5082077 to your computer and use it in GitHub Desktop.
Save leon/5082077 to your computer and use it in GitHub Desktop.
Angular.js jQuery pubsub service
angular.module('app').factory('pubsub', function() {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);
};
$.unsubscribe = function() {
o.off.apply(o, arguments);
};
$.publish = function() {
o.trigger.apply(o, arguments);
};
return $;
});
angular.module('app').controller('GrowlCtrl', ['$scope', 'pubsub', function ($scope, pubsub) {
'use strict';
$scope.notifications = [];
pubsub.subscribe('growl', function (event, messageType, message) {
$scope.notifications.push({
type: messageType || '',
msg: message
});
});
$scope.close = function (index) {
$scope.notifications.splice(index, 1);
};
}]);
angular.module('app').controller('UsageCtrl', ['$scope', 'pubsub', function ($scope, pubsub) {
'use strict';
$scope.click = function () {
pubsub.publish('growl', ['success', 'This is a success!']);
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment