Giter VIP home page Giter VIP logo

onfire.js's Introduction

onfire.js

onfire.js is a simple events dispatcher library (just 0.8kb). simple and usefull.

Can be used in:

  • simple events dispatcher.
  • Cross-component communication of react / vue.js / angular.
  • evnets subscribe and publish.

Live DEMO here. 中文版说明文档点这里

Build Status npm npm npm

API method

1. on(event_name, callback)

Binding / subscribe the event_name with callback. when has event named event_name fired, callback function will process.

The function will return the eventObj, which can be used to cancel bind with API un(eventObj).

2. one(event_name, callback)

Binding / subscribe the event_name with callback only once. And it is will timeout after Trigger.

Only can be Trigger once, then it will be deleted automation.

3. fire(event_name, data1, data2, data3, ...)

Trigger / subscribe the event named event_name, and with datas as the input of callback function.

4. un(eventObj / event_name)

Cancel binding event. You can unbinding a event Object, or just unbinding a event_name.

5. size()

Get the event size.

6. events()

Get the exist events Array.

7. clear()

Clear all the event.

Detail Usage

1. import library

npm install onfire.js

You can import it with <script> tag, or use keyword require or import.

import onfire from 'onfire.js';

// or

var onfire = require("onfire.js");

2. simple usage

Use on to subscribe event, use un to cancel, and use fire to publish / trigger the event.

import onfire from 'onfire.js';
// init the object
onfire = onfire();

// bind event and callback
var eventObj = onfire.on('test_event', function(data1, data2) {
	console.log('this is a event 1');
});
var eventObj2 = onfire.on('test_event', function(data1, data2) {
	console.log('this is a event 2');
});

// fire event
onfire.fire('test_event', 'test_data1', 'test_data2');

// cancel bind event
onfire.un(eventObj); // only cancel the eventObj.
onfire.un('test_event'); // cancel all events named `test_event`.

// size of events
onfire.size();

LICENSE

MIT

onfire.js's People

Watchers

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