0.4.4 • Published 7 years ago

node-observer v0.4.4

Weekly downloads
115
License
GPL
Repository
github
Last release
7 years ago

Observer

This is an observer pattern module

Install

npm install node-observer

Example

Main file: app.js

"use strict";

var observer = require("node-observer");
var hello = require("./hello");

observer.subscribe(this, "HELLO", function(who, data) {
	console.log(data);
});

hello.send();

File: hello.js

"use strict";

var observer = require("node-observer");

var Hello = function() { };

Hello.prototype.send = function() {
	observer.send(this, "HELLO", "hello world!!!");
};

module.exports = new Hello();

Run

node app.js

API

subscribe(object, event, callback)

object:object, event:string, callback:function

subscribe event

unsubscribe(object, event)

object:object, event:string

unsubscribe event

send(object, event, data)

object:object, event:string, data:object

send event 'event' with data 'data'

Browser version

Under client directory there is a browser version of library. Added script tag in your html file. See the example below.

File index.html

<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="observer.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="mylib.js"></script>
</head>
<body>
Javascript observer pattern example: see the result in console
</body>
</html>

File app.js

$(function() {
	Observer.subscribe(this, 'HELLO', function(who, data) {
		console.log(data);
	});
});

File mylib.js

(function(window) {

	var getInstance = function() {
		Observer.send(this, 'HELLO', 'Hello world!!!');
	};

    if (typeof(mylib) === 'undefined') {
		window.mylib = getInstance();
    } else {
        console.log('mylib already defined.');
    }

})(window);

Contributors

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.2.31

9 years ago

0.2.30

9 years ago

0.2.3

9 years ago

0.2.22

9 years ago

0.2.12

9 years ago

0.2.11

10 years ago

0.2.10

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago