jeromeetienne/microevent.js
소개
Node와 브라우저를 위한 20줄짜리 이벤트 이미터 마이크로 라이브러리입니다.
기타 프로젝트측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalMicroEvent.js
MicroEvent.js is a event emitter library which provides the observer pattern to javascript objects. It works on node.js and browser. It is a single .js file containing a 20 lines class (only 321-bytes after minification+gzip).
How to Use It
You need a single file microevent.js. Include it in a webpage via the usual script tag.
<script src="microevent.js"></script>
To include it in a nodejs code isnt much harder
var MicroEvent = require('./microevent.js')
Now suppose you got a class Foobar, and you wish it to support the observer partern. do
MicroEvent.mixin(Foobar)
That's it. The repository contains an example in browser and an example in nodejs. Both use the same code in different contexts. Let me walk you thru it.
Example
First we define the class which gonna use MicroEvent.js. This is a ticker, it is triggering 'tick' event every second, and add the current date as parameter
var Ticker = function(){
var self = this;
setInterval(function(){
self.trigger('tick', new Date());
}, 1000);
};
We mixin MicroEvent into Ticker and we are all set.
MicroEvent.mixin(Ticker);
Now lets actually use the Ticker Class. First, create the object.
var ticker = new Ticker();
and bind our tick event with its data parameter
ticker.bind('tick', function(date) {
console.log('notified date', date);
});
And you will see this output:
notified date Tue, 22 Mar 2011 14:43:41 GMT
notified date Tue, 22 Mar 2011 14:43:42 GMT
...
Conclusion
MicroEvent.js is available on github here under MIT license. If you hit bugs, fill issues on github. Feel free to fork, modify and have fun with it :)
관련 프로젝트 살펴보기
불러오는 중…