Skip to content

volosincu/coffee-pub-sub

Repository files navigation

CoffeePubSub - CoffeeScript/Javascript Publish Subscribe Library

/build/dest/cpubsub.min.js - load with requirejs or directly in a script tag

/build/dest/cpubsub.coffee - CoffeeScript source code


*Prerequisites

  • npm - nodejs package manager (comes with the installation of nodejs)
  • coofee-script npm install -g coffee-script
  • grunt - javascript task runner npm install -g grunt-cli
  • bower - web package manager npm install -g bower

To build the project independently the following steps are required :

  • install the following grunt plugins
     <div>
         <ul>
               <li><code> npm install grunt --save-dev </code></li>
           <li><code> npm install grunt-contrib-coffee --save-dev </code></li>	
               <li><code> npm install grunt-contrib-copy --save-dev </code></li>
               <li><code> npm install grunt-contrib-uglify --save-dev </code></li>
         </ul>
    </div>
    
  • <li>
    
    
           <ul>
                 <li>
                     <div> get the dependencies with bower - jasmine, require </div>
                 </li>
    
                <li>
                    <div> run <code> bower install </code> </div>
                </li>
           </ul>
     </li>
    
    
    
    <li>run <code> grunt default </code> and the build can be found in <code> /build/dest/  </code>  </li>
    <li>coffee files are compiled with <code> grunt default</code>(compile task)</li>
    <li>for development or to run the tests start server(ex: <code> python -m http.server</code>) in folder <code> /build/coffee-pub-sub/  </code>  </li>
    
    <!--
         default: coffee files are compiled with <code> grunt default</code>
    
         call coffee directly to compile files
    
         cd main
    
         $coffee -o ./ -bcw ./main.coffee
         $coffee -o ./core/ -cbw ./src/
         $coffee -o ./specs/js/ -cbw specs/coffee/
    -->
    

Examples:

The library supports 2 ways to publish events :

  • the on - trigger way with a little tweaked (presented below)
  • attaching callbacks to the function properties of object which is achieved by wrapping the original object (Ex: o -> see below) in a proxy that handles the properties calls

the 'on - trigger' way

myobj.trigger(event, params) event - to trigger, params - parameters sent to the callback function

myobj.trigger() called without any parameters the trigger function will return the list of all published callbacks

    var o, cpubsub;
          o = {
            aprop: 'aprop',
            bprop: 'bprop',
            cprop: function(param) {
              return param;
            },
            dprop: 'dprop',
            eprop: function(param) {
              return param;
            },
            sendMessage: function(param) {
              var message = 'the message is : ' + param;
              return message;
            }
          };


          // add topics specific for user in userChannel
          var userChannel = cpubsub.createChannel(o);


      //change name topic
          myobj.on('change-name', function(param) {
            var u = 'You want to change the name to ' + param;
            console.log(u);
            return u;
          });


      //update topic
          myobj.on('update', function() {
            console.log('update'); 
          });
          
          var name = 'Alex';  //the parameter of change-name event              
          var yourRequest = myobj.trigger('change-name', name);
            
          console.log(yourRequest); 

attaching callbacks to the function properties of object

myobj.attachTo(methodName, callback, priority ) methodName - the name of the method to which is attached the callback , callback - the function attached, priority - order of execution (is optional)

 myobj.attachTo('sendMessage', function() {
    console.log('do something here with the properties of the myobj');
  }, 1);

To remove an event use off() method

myobj.off('update'); returns true or false

About

Publish subscribe implementation in CoffeeScript/JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published