angular-primus
Primus provider for Angular.
This plugin works with other Primus plugins like primus-emitter and primus-resource.
Install
Using bower
bower install angular-primus
Usage
angular.module('controllers.primus', ['primus'])
.config(function (primusProvider) {
primusProvider
.setEndpoint('http://mywebsite.com')
.setOptions({
reconnect: {
minDelay: 100,
maxDelay: 60000,
retries: 100
}
})
.setDefaultMultiplex(false);
})
.controller('PrimusCtrl', function ($scope, primus) {
primus.$on('data', function (data) {
$scope.data = data;
});
primus.write('hello');
primus.$on('customEvent', function (customData) {
$scope.customData = customData;
});
primus.$on('account:update', {userId: 23}, function (account) {
_.merge($scope.account, account);
});
primus.send('customEvent', { foo: 'bar' });
primus.$resource('myResource').then(function (myResource) {
myResource.myMethod();
});
});
about $on and $filteredOn
$filteredOn
takes as filter either :
-
an object, whom keys will be deep-matched for correspondance with the 1st param of received data, using lodash matches(...). Example of a deep matching :
primus.$on('node:update', {content: {id: 23, type: 'image'}}, …)
-
a function, taking the received data as arguments and returning true/false = match/don't match
Both $on
and $filteredOn
will call the listener in Angular context, in an optimized way via $evalAsync. So if you have several listeners on the same event, they will all get executed in the same $digest phase.
$filteredOn
will not trigger any apply if the received data doesn't match the given filter. This is desirable if your Angular app is heavy.
License
MIT