Coder Social home page Coder Social logo

angular-tunnels's Introduction

Angular Tunnels

Angular tunnels provides a way of creating a communication tunnel between two or more directives/controllers without requiring them to be part of the same DOM tree (like is needed for $emit and $broadcast);

Installation

To install tunnels, you can either clone this repository directly, or use bower to install:

bower install mrvdot/angular-tunnels

Then include the tunnels.js file into your html and require mvd.tunnels in your module:

angular.module('myApp', ['mvd.tunnels']);

Usage

Tunnels works by registering events within a map that can be injected directly into controllers and directives. You can also use a directive controller for slightly more convenient usage.

Directive usage (Preferred method)

<div id="dir1" my-awesome-directive mvd-tunnel="awesomeness"></div>
<div id="dir2" my-awesome-directive mvd-tunnel="awesomeness"></div>
angular.module('myApp')
    .directive('myAwesomeDirective', function () {
        var count = 1;
        return {
            require : 'mvdTunnel',//Grab the tunnel controller
            link : function ($scope, $element, $attrs, tunnel) {
                count++;//Count how many instances of this directive we have initialized
                //Within directive, we're automatically namespaced to the tunnel attribute value,
                //or '*' global namespace if none is specified
                tunnel.listen('myMessage', function (message, params) {
                    console.log(message);
                    // output: { "tunnel" : "*", "message" : "myMessage" }
                    console.log(params);
                    // output: { "hello" : "tunnels", "count" : 2 }
                });
                
                if (count > 1) {
                    tunnel.send('myMessage', {
                        "hello" : "tunnels",
                        "count" : count
                    });
                }
            }
        }
    })

Direct Map Usage

angular.module('myApp')
    .controller('MyCtrl', function (mvdTunnelMap) {    
        //'*' is the default global namespace, if using the map directly you must specify this
        mvdTunnelMap.listen('*', 'myMessage', function (message, myThought) {
            console.log(message);
            // output: { "tunnel" : "*", "message" : "myMessage" }
            console.log(myThought);
            // output: "Hello tunnels" (first argument passed to send below)
        });
    })
    .controller('OtherCtrl', function (mvdTunnelMap) {
        mvdTunnelMap.send('*', 'myMessage', 'Hello tunnels');
    });

Additional information

###Digest Lifecycle### Much like $emit and $broadcast, all messages sent through the tunnel will be evaluated as part of the Angular scope $digest lifecycle, so any data-model changes made as part of your listeners will be reflected immediately.

###Unsubscribing### Version 0.0.5 brought the ability to unsubscribe, following the same pattern as the built-in $on method. This means the return value of listen is a function that when executed will remove your callback from the subscription list. See below for an example:

var off = mvdTunnelMap.listen('example','message', function () {
  alert("I'm only fired once");
  off();//Unsubscribe
});

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.