aralejs / events Goto Github PK
View Code? Open in Web Editor NEWEvents Utility
Home Page: aralejs.org/events/
Events Utility
Home Page: aralejs.org/events/
demo在此:http://spmjs.io/docs/arale-events-test/examples/index.html
seajs.use('arale-events-test/1.0.0/index', function(araleEventsTest) {
function a(){
};
araleEventsTest.mixTo(a);
a.prototype.say = function(){
this.trigger('say');
};
var b1 = new a();
b1.on('say',function(){
console.log('b1 says');
});
var b2 = new a();
b2.on('say',function(){
console.log('b2 says');
});
b2.say();
//b1 says
//b2 says
});
copy了ieBetter.js的部分代码解决了此问题
https://github.com/zhangxinxu/ieBetter.js/blob/master/ieBetter.js
https://github.com/zhangxinxu/ieBetter.js
if (!Object.keys) {
Object.keys = function(o) {
if (o !== Object(o)) {
throw new TypeError('Object.keys called on a non-object');
}
var k=[], p;
for (p in o) {
if (Object.prototype.hasOwnProperty.call(o,p)) {
k.push(p);
}
}
return k;
};
}
if (typeof Array.prototype.forEach != "function") {
Array.prototype.forEach = function (fn, scope) {
var i, len;
for (i = 0, len = this.length; i < len; ++i) {
if (i in this) {
fn.call(scope, this[i], i, this);
}
}
};
}
Hi,aralers :)
最近考虑使用arale的events,但发现在trigger的时候,对for循环中的apply没有异常捕获。这样的话,如果注册上来的某个callback出现异常,那么callback list中后续的所有回调函数都将得不到执行。
个人认为这样不够合理,所以有后续建议,仅为抛砖。apply放入try catch中,出现异常立即处理,考虑以下处理办法
注:原来在写页游时遇到的一个情景:我们游戏的战斗逻辑等各种复杂逻辑是通过events来处理的,这样效果很好,当所有逻辑与渲染开发完毕后,通过event的形式挂载了音效模块,即对各种战斗事件挂载播放不同音乐。但如果音乐播放出现异常,基于event的其他游戏逻辑不会受到影响。
注:本来想写个test验证一下,但是tests中的runner跑不起来,http://aralejs.org/tools/seajs-helper.js 与 http://aralejs.org/tools/jasmine-runner.js 都是404。spm用的不是很熟,是不是我用错了。
events.js 中使用循环截取传入的回调函数:
// Fill up `rest` with the callback arguments. Since we're only copying
// the tail of `arguments`, a loop is much faster than Array#slice.
for (i = 1, len = arguments.length; i < len; i++) {
rest[i - 1] = arguments[i]
}
使用 Array.prototype.slice
会使代码更精简,为什么说循环的效率会更高?
是否需要增加这个 api
var e = new Events();
e.once('event', callback);
e.trigger('event');
e.trigger('event'); // 不执行
if (isFunction(receiver)) {
for (var key in proto) {
if (proto.hasOwnProperty(key)) {
receiver.prototype[key] = proto[key]
}
}
Object.keys(proto).forEach(function(key) {
receiver.prototype[key] = proto[key]
})
}
是不是考虑 try-catch 一下
try{
Object.keys(proto).forEach(function(key) {
receiver.prototype[key] = proto[key]
})
}catch(e){
for (var key in proto) {
if (proto.hasOwnProperty(key)) {
receiver.prototype[key] = proto[key]
}
}
}
需求来自于 aralejs/base#5
.trigger
返回 callback 的状态值,而不返回 this。
类似jQuery对domready的事件处理形式。当这个事件被触发后,再绑定该事件,处理函数会直接被触发。
比如新建一个类的实例,实现需要异步加载资源(有cache),之后才能用,这时候会有一个ready事件。
但是,同一个页面周期,再次new一个实例,这时候,因为cache,资源不在需要异步加载,在新建的代码片段中,就会直接trigger('ready')。那么在新建这个实例的代码后,再on('ready'),就不会执行了。
虽然有各种办法能解决这问题,但是觉得这算个有必要的功能。
昨天话题的延伸。
如果有一个callback,同时绑定了某个事件(如a)和all,那么触发a或者all的时候,是否也应该只执行一次?
var e = new Events();
function cb(){console.log(arguments)}
e.on('a', cb)
e.on('all', cb)
//分别触发
e.trigger('all')
e.trigger('a')
添加一个别名 emit
,等效于 trigger
。
var evt = new Events();
var name = "event-name";
function handler(){}
evt.on(name, handler); // 绑定第一次
evt.on(name, handler); // 绑定第二次
evt.emit(name); // handler 执行两次
evt.off(name, handler); // 解绑一次
evt.emit(name); // handler 不执行。
这个逻辑是否不正常?解绑时,在找到第一个 handler 后是否应该 break ?
先贴出代码
Events.mixTo = function(receiver) {
receiver = receiver.prototype || receiver;
var proto = Events.prototype;
for (var p in proto) {
if (proto.hasOwnProperty(p)) {
receiver[p] = proto[p];
}
}
};
有一个比较少见(甚至可以说是错误的)代码可能导致它失效。
var foo = {};
foo.prototype = {a:1};
Events.mixTo(foo);
// foo.on(...) 就挂了
各位大神觉得是否需要修复这个 bug 呢,用一个 type()
来判断而不是 prototype
。
我根据需要new 5个Line 组件(同样使用 aralejs-widget),我本来打算在Line 组件里面,setup里面生成 画line的html,但是需要在new的时候就要输入element的名字,否则不能使用event 的方法,
能不能 动态指定 element?
存在一种情况,模块A触发了trigger事件;但是模块B是在A触发trigger的时候才进行on的绑定,所以导致模块B丢失了事件
Events.mixTo
方法不应该将 trigger
方法暴露。trigger
方法应该只提供给内部使用。define("ModuleA", functioin(){
var ModuleA = function(){};
Events.mixTo(ModuleA);
module.exports = ModuleA;
});
var a = new ModuleA();
// 触发事件是对象内部逻辑、状态,a 对象不应该拥有 `trigger` 方法。
看到 trigger 的源码中这些写:
// Execute event callbacks.
returned = triggerEvents(list, rest, this) && returned
// Execute "all" callbacks.
returned = triggerEvents(all, [event].concat(rest), this) && returned
那么使用 object.trigger('all')
的时候,对应的回调应该会被调用两次,只是第二个回调的参数中多了一个事件名。
请问这样的设计是出于什么考虑,还是我的理解有误。
考虑下面的情景:
这样defer一下这个callback的apply,就可以简单实现了。
当然,注册事件的时候可以直接挂载一个带有defer特性的函数, 但是觉得如果能配置会更方便一些。
例如on的时候传个参数{defer:true}.
注:如果传个config形式的参数,就可以附加更多的特性了,例如:
{
defer : true, //推迟执行
once : true, //只执行一次
onerror : DEFAULT_ERROR_HANDLE,
blah blah....
}
这样符合arale的理念吗?
因为方法当中有 Object.keys(proto)方法 所以似乎不兼容IE7
xx.on('event1 event2 event3', handle);
handle 里无法得知是哪个事件触发的handle
一个简单的方式: 对handle追加最后一个参数 即 eventType
xx.on('event1 event2 event3', function(){
var eventType = arguments[arguments.length - 1];
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.