3 Star 4 Fork 3

小为 / onfire.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

onfire.js

onfire.js 是一个很简单的事件分发的Javascript库(仅仅 0.9kb),简洁实用。

可以用于:

  • 简单的事件分发;
  • 在 react / vue.js / angular 用于跨组件的轻量级实现;
  • 事件订阅和发布;

在线示例,点击这里;English Version Readme. Click Here

Build Status npm npm npm

API 方法

1. on(event_name, callback, context)

绑定事件,参数为 event_namecallback, 当有名字为event_name的事件发生的时候,callback方法将会被执行。

这个方法会返回一个eventObj,这个可以用于使用un(eventObj)方法来取消事件绑定。

2. one(event_name, callback, context)

绑定(订阅)事件,参数为 event_name with callback. 当被触发一次之后失效。只能被触发一次,一次之后自动失效

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

异步方式触发名字为 event_name 的事件,并且赋予系列变量datascallback方法的输入值。

4. fireSync(event_name, data1, data2, ...)

同步的方式触发名字为 event_name 的事件,并且赋予系列变量datascallback方法的输入值。阻塞。

5. un(eventObj / event_name)

取消事件绑定。可以仅仅取消绑定一个事件回调方法,也可以直接取消全部的事件。

6. clear()

清空所有事件。

使用的DEMO

1. 引入js文件

npm install onfire.js

可以使用 <script> 标签直接引入; 也可以使用 require 或者 import 关键字引入,会得到全局变量 onfire。

import onfire from 'onfire.js';

// or

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

2. 简单使用

使用方法 on 来订阅事件, 使用 un 来取消订阅, 使用 fire 方法来触发事件。

import onfire from 'onfire.js';

function test_callback(data1, data2) {
	console.log('this is a event 1');
}

// 绑定事件
var eventObj = onfire.on('test_event', test_callback);
var eventObj2 = onfire.on('test_event', function(data1, data2) {
	console.log('this is a event 2');
});

// 触发事件
onfire.fire('test_event', 'test_data1' 'test_data2');

// 取消绑定
onfire.un(eventObj); // 取消绑定这个事件.
onfire.un('test_event'); // 取消绑定所有的 `test_event`.
onfire.un(test_callback); // 取消绑定所有的 `test_callback` 方法.

LICENSE

MIT

The MIT License (MIT) Copyright (c) 2016 Hust.cc 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. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

:gun: onfire.js is a simple events dispatcher subscribe / publish library (just 0.7kb). simple and usefull. 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/hustcc/onfire.js.git
git@gitee.com:hustcc/onfire.js.git
hustcc
onfire.js
onfire.js
master

搜索帮助