0

I try to create a method that can be called from the created DOM element corresponding to my B class, like that:

$("#" + data.selectedId).eventCallback(data);

My eventCallback is defined in a mother class A, as you can see in the following code:

function A (){
    this.addToPage = function () {
        ...

        var context = this;
        // This call is well overridden
        context.onEvent("toto");

        jQuery.fn.extend({
            // This call uses A's method every time
            eventCallback: function (data) {context.onEvent(data);}
        }); 
    };

    this.onEvent = function (data) {
         console.log("to override");
    };
}

function B (){
    this.onEvent = function (data) {
         console.log("overridden");
    };
}
B.prototype = Object.create(A.prototype);

The problem is, as commented, that I seem to not being able to use the same context when I enter the block jQuery.fn.extend. Is there a better (and cleaner) way to do it? Do I miss something?

Edit, to clarify:

  • The A class define the structure of widgets which are set in my html document (so to my mind an A instance is somehow linked to a part of the DOM).

  • As a user, I want to be able to select a widget that call a method (which definition is depending on B).

So My idea was to implement a callBack in the class and then make it callable from the DOM objects created with an A instance.

aveuiller
  • 1,511
  • 1
  • 10
  • 25
  • You don't want `A`-instance-specific functions lurking around at the "global" `$.fn`. Really not. What are you actually trying to do with this? – Bergi Jun 11 '14 at 14:15
  • Btw, [your `B.prototype` assignment is borken](http://stackoverflow.com/q/12592913/1048572) – Bergi Jun 11 '14 at 14:16
  • What I whant is pretty simple, that's only calling the onEvent(...) callback by selecting any object of type A from the DOM. – aveuiller Jun 11 '14 at 14:22
  • "*Selecting any object of type A from the DOM*" - but As are js objects, not dom nodes? please elaborate further, maybe a code example. – Bergi Jun 11 '14 at 14:29

0 Answers0