lasso-js

Advanced mediator library

npm install lasso-js
1 downloads in the last week
1 downloads in the last month

Lasso

Lasso is small library that helps to decouple your application into separated units of code that don't depend on each other. Library implements mediator pattern with advanced broadcast / listen (publish / subscribe) system.

How to use?

Basic usage


//subscribe callback for 'hello-event' event
lasso.listen("hello-event", function(name) {
    alert("Hello, " + name + "!");
});

//fire event and pass additional parameter
lasso.broadcast("hello-event", "Lasso");

//the same for 'publish' and 'subscribe' methods
lasso.subscribe("the-same-for-subscribe", function(name) {
    alert("Hello, " + name + "!");
});
lasso.broadcast("the-same-for-subscribe", "Lasso");

Subscribe callback for few events


lasso.listen("event1, event2", function() {
    alert("works!")
});

lasso.broadcast("event1, event2");

//as objects

lasso.listen({
    "event1": function(obj) {
        alert("Handler for event1, with " + obj.foo);
    },
    "event2": function(arg1, arg2, arg3) {
        alert("Handler for event2, with " + arguments.join(","));
    }
});

lasso.broadcast({
    //fire event1 and pass object
    "event1": {
        foo: "bar"
    },
    //pass array with numbers
    "event2": [1,2,3]
});

Get listeners


//Create 3 handlers for the same event
lasso.listen("firstEvent", function() {
    return 1;
});
lasso.listen("firstEvent", function() {
    return 2;
});
lasso.listen("firstEvent", function() {
    return 3;
});
//add another event handler
lasso.listen("anotherEvent", function() {
    return "i am not like others!";
});

//Returns an array with method handlers for 'firstEvent'
var firstEventHandlers = lasso.listeners("firstEvent");
firstEventHandlers.length == 3 ; // true


//without params returns all methods in mediator
var allEvents =lasso.listeners();
allEvents.length == 4 ; // true

Clear subscribers

//removes all events, clears mediator
lasso.clear();

//removes all subscribers for specified event
lasso.clear("eventName"); //or
lasso.clear("eventName, otherEvent");

//removes specified handler
function handler() {
    return "i am method!";
}
lasso.clear("eventName", handler);

Subscribe for events made from specific object.


var broadcaster = {
    name: "Max"
};

lasso.from(broadcaster).listen("event", function() {
    alert("I am listening only broadcaster object!");
});

lasso.broadcast("event"); // wont work

lasso.from(broadcaster).broadcast("event"); //works perfect

Use with favorite library


$.lasso.listen("foo", function(name) {
    alert("Hello, " + name + "!");
});

$.lasso.broadcast("foo", "bar");

Backbone.Mediator.subscribe('foo', function() {
    alert("Just works!")
});

//or integrate into any library or object
var library = {
    //the best code ever
}

lasso.into(library); //or lasso.integrate(library)

library.listen('foo-event', function(bar) {
    alert(bar);
});
library.broadcast('foo-event', "bar");

Install

> npm install lasso-js

License notes (MIT License)

Copyright (c) 2012 Max Podriezov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

npm loves you