Calling a method from XBL
Asked Answered
M

2

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.

Myrilla answered 21/12, 2010 at 13:1 Comment(6)
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.Rose
@Sergey Ilinsky even more sad, becouse it don't work calling with this, it'll work just on the constructor, not on methods.Myrilla
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.Delitescent
@Delitescent 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..Myrilla
@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.Delitescent
@Delitescent 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!Myrilla
D
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..

Delitescent answered 22/12, 2010 at 15:5 Comment(0)
D
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>
Dominations answered 10/1, 2011 at 21:8 Comment(2)
+1 Interesting.. even more interesting to know this "implements" attribute, is there an "extends" one?Myrilla
Extending what? A binding can extend another binding, if that's what you mean.Dominations
D
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..

Delitescent answered 22/12, 2010 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.