Giter Site home page Giter Site logo

cubicle.js's Introduction

Cubicle.js

A small light-weight javascript library for defining modular components, 4kb unminimized (108 loc) as of Dec 16 - 2014.

Getting Started

You can include Cubicle.js through rawgit.com url

<script type="application/javascript" src="https://cdn.rawgit.com/nripendra/Cubicle.js/v0.2.0/Cubicle.min.js"></script>

Or, simply download the Cubicle.js file and place on your server, and link to it, if you want to self-host the file. I'm planning for a nuget package for visual-studio users, but not quite there yet.

Introducing cubicle.

A cubicle is a small container that provides certain amount of privacy. Defining a cubicle is as easy as this:

cubicle(function(invite, announce){
  
});

Just call 'cubicle' function, and pass an anonymous function as shown above. The annonymous function gets two helper functions as the parameter: invite and announce. The 'invite' function can be used to get hold of worker in other cubicles, provided that the worker in other cubicle has announced itself.

What is a worker?

A worker is the main module that works inside the cubicle. The worker is the main thing that does the actual work. A worker can have 'init' method, for the purpose of constructor. And the anonymous function should always return the worker, e.g:

cubicle(function(invite, announce){
  var worker = {
    "init": function(){
        // this is the constructor..
    }
  };
  
  return worker;
});

Announcing a worker

In a normal situation worker cannot work alone, it will require help from other workers, and it needs to help others in return. The way for worker to announce that it is available for helping others is to announce itself, as shown here:

cubicle(function(invite, announce){
  var worker = {
    "init": function(){
        // this is the constructor..
    },
    sayHi: function(){
        alert("Hi!!");
    }
  };
  
  return announce("worker1", worker);
});

Note that we are returning as soon as calling announce, it is possible because announce returns worker instance once it has registered the worker in list of available workers.

Inviting a worker

As mentioned earlier one worker alone may not be able to do all the work, so it needs to invite worker in other cubicle for help. Here's and example which calls worker1 above inside another cubicle.

cubicle(function(invite, announce){
  var worker = {
    "init": function(){
        var helper = invite("worker1");
        helper.sayHi();
    }
  };
  
  return announce("worker2", worker);
});

It is very important to notice that both cubicles never messes with global namespace (window object in case of browser), the main idea behind cubicle is to provide this isolation, while modules are able to cooperate with each other.

But I want to mess the global namespace :P

Absolutely!! Some times we may want to create cubicle that is not all pervasive, i.e. allow the module (worker) to be available anywhere out side cubicles. Just pass true as the last parameter in announce, like this:

cubicle(function(invite, announce){
  var worker = {
    "init": function(){
        
    },
    doWork: function(){
        alert("phew!!");
    }
  };
  
  return announce("globalWorker", worker, true);
});

globalWorker.doWork();

As, shown above globalWorker is announced in global context (by passing true as last parameter). Hence, 'globalWorker' now becomes a global variable that doesn't require inviting.

License

This library is released under MIT license.

cubicle.js's People

Contributors

nripendra avatar

Watchers

James Cloos 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.