Code Monkey home page Code Monkey logo

angular-primus's Introduction

angular-primus Build Status

This plugin is no longer actively maintained, you can still use it but issues will not be resolved. If you want the npm name, you can contact me by email.

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
  // Define Primus endpoint.
  .setEndpoint('http://mywebsite.com')
  // Define Primus options.
  .setOptions({
    reconnect: {
      minDelay: 100,
      maxDelay: 60000,
      retries: 100
    }
  })
  // Define default multiplex option for resources.
  .setDefaultMultiplex(false);
})
.controller('PrimusCtrl', function ($scope, primus) {

  // Listen "data" event.
  primus.$on('data', function (data) {
    $scope.data = data;
  });

  // Write data.
  primus.write('hello');


  // Listen custom event using primus-emitter.
  primus.$on('customEvent', function (customData) {
    $scope.customData = customData;
  });

  // Listen custom event with a filter (more details below)
  // ex. server broadcasting a user account update :
  primus.$on('account:update', {userId: 23}, function (account) {
    _.merge($scope.account, account);
  });

  // Send data using primus-emitter.
  primus.send('customEvent', { foo: 'bar' });


  // Use resource with primus-resource.
  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

angular-primus's People

Contributors

eiriksm avatar gregberge avatar yvem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.