Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery bind/trigger in a prototypal inheritance scenario. Can't trigger events on the right object
    primarykey
    data
    text
    <p>I'm trying to wrap my head around using bind/trigger in an inheritance scenario. Here is what I got:</p> <pre><code> var MyApp = {}; MyApp.GrandParent = function (){ var self = Object.create({}), $self = $(self); self.doFoo = function(){ console.log('Just to asure we can call from child to grandparent'); $(this).trigger('Happened'); //$self.trigger('Happened'); //Why doesn't it work this way }; return self; } MyApp.Parent = function(){ var self = Object.create(MyApp.GrandParent()), $self = $(self); self.doSomething = function(){ console.log('Just to asure we can call from child to parent'); $(this).trigger('SomethingHappened'); //$self.trigger('SomethingHappened'); //Why doesn't it work this way }; return self; } MyApp.Child = function(){ var self = Object.create(MyApp.Parent()), $self = $(self); $self.bind('SomethingHappened', function(){ console.log('Client logs: SomethingHappened'); }); $self.bind('Happened', function(){ console.log('Client logs: Happened'); }); return self; } var foo = new MyApp.Child(); foo.doSomething(); foo.doFoo(); </code></pre> <p>Working Example: <a href="http://jsfiddle.net/cburgdorf/8fWta/12/" rel="nofollow">http://jsfiddle.net/cburgdorf/8fWta/12/</a></p> <p>As you can see, we have an inheritance chain starting by the grandparent, over the parent to the child. It works. However, I just don't understand why I can't trigger the events on the $self object. It feels wrong to fire them on $(this).</p> <p>What I want to archieve is to keep the prototype chain working and fire the events on the $self object. I guess I need to use something like $.proxy maybe but I really don't have a clue.</p> <p>I know I could also do it like this <a href="http://jsfiddle.net/cburgdorf/8fWta/11/" rel="nofollow">http://jsfiddle.net/cburgdorf/8fWta/11/</a> but I would rather keep the prototype chain working...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload