javascript - How to emit an event with jquery? - Stack Overflow

i'm trying to create a simple class to emit an event in certain moment.I get this javascript erro

i'm trying to create a simple class to emit an event in certain moment. I get this javascript error : this.emit is not a function

This is the code:

var class_test = function(){};
class_test.prototype = {
   some_property: null,

    doSomething: function(msg) {
            this.some_property = msg;
            this.emit("somethingHappened");
        }
};


var test = new class_test();
test.doSomething('Example');

test.addEventListener('somethingHappened',function(){
    alert("Event");
},false);

Any suggestion?,Thanks!

i'm trying to create a simple class to emit an event in certain moment. I get this javascript error : this.emit is not a function

This is the code:

var class_test = function(){};
class_test.prototype = {
   some_property: null,

    doSomething: function(msg) {
            this.some_property = msg;
            this.emit("somethingHappened");
        }
};


var test = new class_test();
test.doSomething('Example');

test.addEventListener('somethingHappened',function(){
    alert("Event");
},false);

Any suggestion?,Thanks!

Share Improve this question asked Mar 14, 2014 at 14:31 hanskaithanskait 451 silver badge9 bronze badges 4
  • 2 That code is not using jQuery at all. – Frédéric Hamidi Commented Mar 14, 2014 at 14:32
  • @FrédéricHamidi the class is wrong? – hanskait Commented Mar 14, 2014 at 14:38
  • @hanskait: Do you know what jQuery is? And emit() es from NodeJS. Is that what you're using? – cookie monster Commented Mar 14, 2014 at 14:40
  • 1 @hanskait, well, it does not have the emit() method you're trying to call. If you wish to emit events with jQuery, I would suggest you use the library (on() instead of addEventListener() for instance) and refer to the trigger() method. – Frédéric Hamidi Commented Mar 14, 2014 at 14:41
Add a ment  | 

1 Answer 1

Reset to default 5

with jQuery ,since you requested it :

var class_test = function(){
   this.dispatcher = $({});
};
class_test.prototype = {
   some_property: null,

    doSomething: function(msg) {
            this.some_property = msg;
            this.dispatcher.trigger("somethingHappened");
        }
};


var test = new class_test();


test.dispatcher.on('somethingHappened',function(){
    alert("Event");
});

test.doSomething('Example');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318114a4421251.html

相关推荐

  • javascript - How to emit an event with jquery? - Stack Overflow

    i'm trying to create a simple class to emit an event in certain moment.I get this javascript erro

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信