0

From a XBL method, when I need to call another method, I do like:

        <method name="myMethod_1">
            <body>
                <![CDATA[
                    // do staff
                ]]>
            </body>
        </method>


        <method name="myMethod_2">
            <body>
                <![CDATA[
                    document.getElementById("thisElementID").myMethod_1();
                ]]>
            </body>
        </method>

I would like to know if is there a way to call the local method without need the element id? I've tried this.myMethod_1() but it says the method don't exist.

The Student
  • 27,520
  • 68
  • 161
  • 264
  • this.myMethod_1() Should just work. The way you do it now breaks bigger part of XBL paradigm - your binding is not reusable any more. – Sergey Ilinsky Dec 21 '10 at 15:27
  • @Sergey Ilinsky even more sad, becouse it don't work calling with this, it'll work just on the constructor, not on methods. – The Student Dec 21 '10 at 16:55
  • can you show us code calling myMethod_2? If you call it like: document.getElement(...).myMethod_2() that's fine, but if you have something like someElement.addEventHandler("click", myxbl.myMethod_2,...); that won't work since event target will be this. – Mihailo Dec 22 '10 at 10:53
  • @Mihailo I'm calling it as my example shows. But, when making a simple example project, it just worked with `this`.. I don't know why in my current project's method it does not.. I'll make some tests and I back here.. – The Student Dec 22 '10 at 12:09
  • @Tom - you didn't actually answer how myMethod_2 is called, you showed how myMethod_1 is called. This is important for determining what is _this_ in that context. – Mihailo Dec 22 '10 at 14:09
  • @Mihailo ow, think I got it.. it's exactly this the problem.. I'm calling it from a keypress listener of another document, and the "this" was not what I think.. post it as an answer for me to mark it as right. And thanks! – The Student Dec 22 '10 at 14:43

2 Answers2

1

In the specific case of an event listener, there is another way around the problem, and that is to pass the element itself as the listener. Of course you only get one handleEvent method, so this is less useful if you're listening to lots of different events on lots of different event targets.

<implementation implements="nsIDOMEventListener">
  <method name="handleEvent">
    <parameter name="aEvent"/>
    <body>
      <![CDATA[
        // do stuff
      ]]>
    </body>
  </method>
Neil
  • 54,642
  • 8
  • 60
  • 72
0

can you show us code calling myMethod_2? If you call it like: document.getElement(...).myMethod_2() that's fine, but if you have something like someElement.addEventHandler("click", myxbl.myMethod_2,...); that won't work since event target will be this.

This is important for determining what is this in that context

EDIT: (Tom's reply)

ow, think I got it.. it's exactly this the problem.. I'm calling it from a keypress listener of another document, and the "this" was not what I think..

Mihailo
  • 754
  • 3
  • 6