jgallen23 / fidel

A basic javascript controller
MIT License
36 stars 4 forks source link

Can't call emit outside of anonymous function or proxy #11

Open listenrightmeow opened 10 years ago

listenrightmeow commented 10 years ago

Only test cases 3 & 4 will pass - on all branches including v3

var Test = fidel.define('test', {
  init : function() {
    var self = this;

    this.emit('test1');

    this.trigger('test2');

    $('body').on('test3', function(){
     self.emit('test3');
    });

    $('body').trigger('test3');

    setTimeout(function(){
      self.emit('test4');
    });
  },
  trigger : function(str) {
    this.emit(str);
  }
});

var test = new Test();

test.on('test1', function() {
  console.log('test1 callback');
});

test.on('test2', function() {
  console.log('test2 callback');
});

test.on('test3', function() {
  console.log('test3 callback');
});

test.on('test4', function() {
  console.log('test4 callback');
});
listenrightmeow commented 10 years ago

I don't have permissions to the repo to fire a pull request.

   Fidel.prototype.emit = function(eventName, data, namespaced) {
-    var ns = (namespaced) ? this.namespace : '';
-    this.el.trigger(eventName+ns, data);
+    var self = this,
+        ns = (namespaced) ? this.namespace : '';
+
+    setTimeout(function() {
+      self.el.trigger(eventName+ns, data);
+    }, 0);
   };