Hmmm.  We all love organization (hooray), but it's how does this
organization help you be more productive that counts.  Your tendency to put
things in one place is a very good one.  Developers will often say "Once and
only once" or "Don't repeat yourself" as mantras to remember to keep our
code organized, and refer to things rather than copy and paste things.
 Bottom line is you're going to have to graduate to the next level of skills
to get what you're after.  Let me see if I can help out.
You want to share all the definitions of services between multiple mxml
files?  Am I right?  Well putting them in your main mxml file isn't going to
work for you.

1.  Create an actionscript file (not mxml).  Then you can instantiate this
file in your main as well as other places. (even in mxml).  The difference
is you can't specify the results and error callbacks like you did in mxml.
 If you're consolidating all of your web service calls then do something
like:

import mx.rpc.http.HTTPService;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;

[Event( name='myEvent', type='mx.events.DynamicEvent')]
public class MyService {

   private var _host : String;  // this is so you don't hard code the
server you're talking too.  This is nice for development vs.
production
   public function get host() : String { return _host; }
   public function set host( value : String ) : void { _host = value; }

   public function callARemoteService( params : Object ) : void {
        var service : HTTPService = new HttpService();
        service.url = "http://"; + host + "/foobar";
    service.addEventListener( ResultEvent.RESULT, function( event :
ResultEvent ) : void {
            var evt : DynamicEvent = new DynamicEvent('myEvent');
            evt.data = service.data;
            dispatch( evt );
        });
    service.send(parameters);
   }

}

Now inside your main mxml you can use this actionscript class at a
higher level than the old httpservice one like so:

private function callService() : void {
   myService. callARemoteService( { arg1: 'Yo', arg2: 'Dude!' } );
}

private function someCallback( event : DynamicEvent ) : void {
   // do something with event.data here!
}

<local:MyService id="myService" host="localhost" myEvent="someCallback" />

For every different URL you want to call on your server create a
method in MyService.  Now any place that uses MyService in their mxml
can call any method in your MyService class, and get results back.
Remember each method must dispatch a different event name (aka
'myEvent' in this case), and you'll want to delcare them in the
[Event()] metatdata so Flex will recognize it.

Once you start sending back structured data (JSON, XML, etc). you can
even encapsulate that parsing and logic inside MyService and reuse
that as well.

Good luck,

Charlie


On Fri, Oct 23, 2009 at 3:10 PM, Sarah Davidson <[email protected]> wrote:

> Charlie,
> I was trying to be organized and have all my webservices with all of its
> operations listed out in the main mxml file, but I can't. It causes errors
> because some of the operations have results that call things not on the
> page. I end up having to delete the ones not used on that page and copying
> the webservice with just the operations for a component to the component's
> file. I really don't like having things all over the place.
> Sarah
>
>
> On Fri, Oct 23, 2009 at 2:58 PM, Charlie Hubbard <
> [email protected]> wrote:
>
>> I just reread your 2nd question.  I'm not sure what you mean by a "place
>> to put your WebServices so I don't have to copy different pieces...".
>> Are you asking about where your server side code belongs?  What exactly do
>> you think you need to copy between pieces?  And what are those pieces?
>>
>> Charlie
>>
>> On Thu, Oct 22, 2009 at 10:21 PM, Sarah Davidson <[email protected]>wrote:
>>
>>> Hi.... this is the first time I've tried this and I'm just beginning with
>>> Flex. Please excuse what may seem like silly questions :)
>>> For most things I have been trying to do, I have been able to find
>>> solutions online or in my books, but the one thing that I can't find any
>>> good and consistent information on is organizing everything. All the
>>> examples work with just one file. So far there are two things that I'm
>>> trying to figure out (since my project has lots of files):
>>>
>>>    - A folder structure (design pattern?). I want to be tidy.
>>>    - A place to put all my WebServices so I don't have to copy different
>>>    pieces to every single component. Is there a way to do some kind of 
>>> include
>>>    that references a file that lists all my services?
>>>
>>> Thank you!
>>>
>>>
>>
>

Reply via email to