Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger 触发的是所有实例上的on绑定么? #17

Open
airyland opened this issue Jun 30, 2014 · 15 comments
Open

trigger 触发的是所有实例上的on绑定么? #17

airyland opened this issue Jun 30, 2014 · 15 comments

Comments

@airyland
Copy link

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 

});
@army8735
Copy link
Member

prototype方法中的this指向是当前实例对象,可不是所有。

@airyland
Copy link
Author

但是按照上面的代码,已经触发了其他实例的on绑定了

@army8735
Copy link
Member

啊哦,mixTo居然共享了event对象

@army8735
Copy link
Member

mixTo到一个function上有这个问题,建议类使用继承,而对象用mixTo

@airyland
Copy link
Author

我之前花了两夜调试这个问题。。
以为是自己的问题。最后自己直接写个简单的event函数了。

那么会不会修复?怀疑没人这样用出问题么。。

@army8735
Copy link
Member

感觉应该限制mixTo仅为对象,因为function时放到其prototype上了,结果所有实例共用。

@airyland
Copy link
Author

像emitter一样 http://github.com/component/emitter
mixTo(prototype)是不是比较合理。

var Emitter = require('emitter');
Emitter(User.prototype);

@army8735
Copy link
Member

它也绑到prototype上了?那不也是一样?

@airyland
Copy link
Author

不会,它可以正常使用。

@army8735
Copy link
Member

知道它的办法了……
不过如果类有_callbacks属性会被覆盖,隐藏了个坑。

@afc163
Copy link
Member

afc163 commented Jun 30, 2014

各种坑啊,要不要回滚一下。

@airyland
Copy link
Author

@army8735 我觉得这是个约定的问题,就像现在文档中约定了event名不能为all

army8735 added a commit that referenced this issue Jun 30, 2014
@rdmclin2
Copy link

== 神坑...,今天用的时候遇到了,希望能够改进...哪怕是文档里说一下

@airyland
Copy link
Author

@rdmclin2 2014年的issue。同学你这是挖坟啊,不过这个issue居然还是open状态。。

@rdmclin2
Copy link

@airyland ==, 因为一直没有解决,于是我被坑到了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants