3 Replies Latest reply on Jan 29, 2008 9:03 PM by Ratsnackbar

    Webservice Call Not Firing

    VitaltypeMike
      I am trying to understand eventlisteners. I am trying to have my submit button fire off a click event which takes the information from the input boxes and submits them to the webservice. The webservice is not getting any data and I have tested that and I know it is working. I believe it is my eventlistener setup that is causing me some problems. Any ideas on what I am doing wrong? Thanks, Mike

      import mx.rpc.soap.WebService;
      private function initApp():void
      {
      btnSubmit.addEventListener("click", newOwner);
      }

      private function newOwner():void
      {
      var wsOwners:WebService = new WebService();
      wsOwners.wsdl = " http://localhost:1711/Owners/Owners.asmx?WSDL";
      wsOwners.loadWSDL();
      var result:String = wsOwners.NewOwner(tbxFirstName.text,
      tbxLastName.text,
      tbxAddress1.text,
      tbxAddress2.text,
      tbxCity.text,
      tbxState.text,
      tbxZip.text,
      tbxCountry.text,
      tbxPhone.text,
      tbxFax.text,
      tbxEmail.text);
      wsOwners.addEventListener("Success", AddOwner);
      wsOwners.addEventListener("Failure)", FailedToAddOwner);
      }

      private function AddOwner():void
      {
      lblResult.text = "The owner was created successfully";
      }

      private function FailedToAddOwner():void
      {
      lblResult.text = "There was a problem adding the owner";
      }
      ]]>
      </mx:Script>
      <mx:Panel width="276" height="436" layout="absolute" id="pnlNewOwner" title="Create a New Owner">
      <mx:TextInput x="83" y="10" id="tbxFirstName"/>
      <mx:Label x="10" y="12" text="First Name:" textAlign="right"/>
      <mx:TextInput x="83" y="38" id="tbxLastName"/>
      <mx:Label x="10" y="40" text="Last Name:" textAlign="right"/>
      <mx:TextInput x="83" y="66" id="tbxAddress1"/>
      <mx:Label x="10" y="68" text="Address:" textAlign="right"/>
      <mx:TextInput x="83" y="94" id="tbxAddress2"/>
      <mx:Label x="10" y="96" text="Address 2:" textAlign="right"/>
      <mx:TextInput x="83" y="122" id="tbxCity"/>
      <mx:Label x="10" y="124" text="City:" textAlign="right"/>
      <mx:TextInput x="83" y="152" id="tbxState"/>
      <mx:Label x="10" y="154" text="State:" textAlign="right"/>
      <mx:TextInput x="83" y="180" id="tbxZip"/>
      <mx:Label x="10" y="182" text="Zip:" textAlign="right"/>
      <mx:TextInput x="83" y="208" id="tbxCountry"/>
      <mx:Label x="10" y="210" text="Country:" textAlign="right"/>
      <mx:TextInput x="83" y="236" id="tbxPhone"/>
      <mx:Label x="10" y="238" text="Phone:" textAlign="right"/>
      <mx:TextInput x="83" y="264" id="tbxFax"/>
      <mx:Label x="10" y="266" text="Fax:" textAlign="right"/>
      <mx:TextInput x="83" y="292" id="tbxEmail"/>
      <mx:Label x="10" y="292" text="Email:" textAlign="right" height="20"/>
      <mx:Button x="10" y="320" label="Submit" id="btnSubmit"/>
      <mx:Button x="84" y="320" label="Clear" id="btnClear"/>
      <mx:Label x="10" y="350" width="236" id="lblResult"/>
      </mx:Panel>
      <mx:Panel width="1018" height="200" layout="absolute" id="pnlCurrentOwners" title="Current Owners">
      <mx:DataGrid x="10" y="10" width="978">
      <mx:columns>
      <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
      <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
      <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>
      </mx:Application>
        • 1. Re: Webservice Call Not Firing
          VitaltypeMike Level 1
          I just added initialize="initApp()" in the application declaration, but still no change. I added a breakpoint at the btnSubmit.addEventListner, but I never get there when I click the button.
          • 2. Re: Webservice Call Not Firing
            VitaltypeMike Level 1
            I tried changing the eventlistener to this.

            btnSubmit.addEventListener(MouseEvent.CLICK, newOwner);

            But it still won't fire. I did call the function directy by adding this.

            <mx:Button x="10" y="320" label="Submit" id="btnSubmit" click="newOwner()"/>

            So I know this eventListener is not working properly. I am not sure why, I am following a lynda.com example and it doesn't appear to be too difficult.
            • 3. Re: Webservice Call Not Firing
              Ratsnackbar Level 2
              I would stick with the format "btnSubmit.addEventListener(MouseEvent.CLICK, newOwner);" as you stated but I would suggest a couple of changes.

              If this is in MXML (which it appears to be) you will want to make sure to call your init() function after the application/component is created. Using creationComplete="init()" is customary

              Example:
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

              If you have not already you will want to make sure you imported your MouseEvent Class.

              import flash.events.MouseEvent;

              I would also suggest setting up the event listener to use weak references like so.

              this.btnSubmit.addEventListener(MouseEvent.CLICK, newOwner,false,0,true);

              Adding "this" to the front is just a preference of mine but the ",false,0,true" is the part you want to set it to a weak reference. Helps avoid memory leaks.

              As for the function I have found that If I declare the eventListener in actionscript and do not list the event type in the functions constructor flex builder tells me nasty things about my parentage.

              So you would change your function to something like this.

              private function newOwner(event:MouseEvent):void{
              //doSomthing here like
              Alert.show("Yep looks like we got it.");
              }

              Even if you are not going to make use of the event info in the function.

              Really small example would be:
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
              <mx:Script>
              <![CDATA[
              import flash.events.MouseEvent;
              import mx.controls.Alert;

              private function initApp():void{
              this.myBtn.addEventListener(MouseEvent.CLICK, sayHello,false,0,true);
              }

              private function sayHello(event:MouseEvent):void{
              Alert.show("Hello there!");
              }

              ]]>
              </mx:Script>

              <mx:Button id="myBtn" label="My Button" />
              </mx:Application>

              Anyway I hope that helps.

              Oh yes...And one more thing. If this is a component you will want to to make sure to turn off the event listener when the component is no longer needed. I.E.

              private function closeThis():void{
              this.btnSubmit.removeEventListener(MouseEvent.CLICK,newOwner);
              }

              You could call this function as part of the routine to remove the component from the screen for example. Again no use having events hanging out there that are no longer needed.