Aide - Recherche - Membres - Calendrier
Version complète : Utilisation sans _global
Centre de Formation Flash - Forums Adobe Flash > Rich media et intégration > Flex
VinceDDH
Cher communauté !
Je continu mon petit bout de chemin en flex, et je suis toujours bloqué par le disparition du _global....
J'ai plusieurs composants imbriqués les uns dans les autres, et je récupère dans mon fichier mxml principale les valeurs loader par des fichier xml, mais je n'arrive pas à les récupérer ensuite dans mes composants....
J'ai cru comprendre qu'il fallait utiliser les [Bindable], mais je sèche...

Si quelqu'un pouvait venir une fois de plus à mon secours !
Merci d'avance !

Vince
Fusion
Pour l'appli :
CODE
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyClasses="MyClasses.*">

    <MyClasses:MyButton param="300" />

</mx:Application>


Et pour la classe MyButton :


CODE
<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="start()">

<mx:Script>
    <![CDATA[

        public var param:String;
public function start():void
{
    label=param;
}

        ]]>
    </mx:Script>

</mx:Button>


Oublie pas de mettre MyButton.mxml dans un dossier nommé MyClasses pour que ça marche. J'espère que l'exemple t'a aidé...
VinceDDH
Je comprends pas trop ta réponse Fusion.
J'aimerais accéder à partir de zoneRecherche.mxml de ma variable nbAgence, définit dans mon FichierRacine.mxml.
C'est pour ca que j'aurais voulu définir ma variable nbAgence comme un équivalent de _global
(Le titre du POST n'est peut être pas très clair à ce sujet....)
Voici mon code :

FichierRacine.mxml
CODE
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="0x404040" backgroundGradientColors="[0x404040, 0x242424]" xmlns:menu="*" creationComplete="initialisation();">
    <mx:Script>
        <![CDATA[

            import ChargementXML;
            import mx.controls.Alert;

[Bindable] private var agenceXML:Object;
[Bindable] private var nbAgence:Number = new Number();
            private var chargementAgence:ChargementXML = new ChargementXML("agenceXML", "xml/xmlAgence.xml");
        
            private function initialisation():void {
                
                chargementAgence.send();
                chargementAgence.addEventListener(customEvent.COMPLETE, resultatAgence);
            }
            
            private function resultatAgence(event:customEvent):void {
                agenceXML = chargementAgence._contentFile;
                agenceXML.ignoreWhite = true;
                nbAgence = agenceXML.agences.agence.length;
            }
        ]]>
    </mx:Script>
    <mx:Canvas width="900" height="634" horizontalCenter="0" verticalCenter="0">
        <mx:Canvas width="900" height="620" backgroundColor="#999798" y="14" >
            <mx:Accordion id="accordion" width="687" height="558" x="213" y="62">
                <!-- Define each panel using a VBox container. -->
                <mx:VBox label="Accordion Button for Panel 1">
                    <mx:Label text="Accordion container panel 1"/>
                </mx:VBox>
    
                <mx:VBox label="Accordion Button for Panel 2">
                    <mx:Label text="Accordion container panel 2"/>
                </mx:VBox>
            </mx:Accordion>
        </mx:Canvas>
        <menu:zoneRecherche/>
    </mx:Canvas>
</mx:Application>


ChargementXML.as
CODE
package {
    import mx.rpc.http.mxml.HTTPService;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import flash.events.EventDispatcher;
    
    public class ChargementXML extends EventDispatcher {
        
        public var _contentFile:Object;
        private var _nameFile:String = new String();
        private var httpLoader:HTTPService = new HTTPService();
        
        public function ChargementXML(nameVar:Object, nameFile:String):void {
            trace("Appel de la fonction ChargementXML : " + nameVar + " - " + nameFile);
            _nameFile = nameFile;
            
            httpLoader.url = _nameFile;
            httpLoader.showBusyCursor = true;
            httpLoader.addEventListener(ResultEvent.RESULT, chargementFini);
            httpLoader.addEventListener(FaultEvent.FAULT, erreurChargement);
        }
        
        public function send():void {
            trace("Lancement du chargement du fichier : " + _nameFile);
            httpLoader.send();
        }
        
        private function chargementFini(event:ResultEvent):void {
            trace("Téléchargement terminé du fichier : " + _nameFile);
            _contentFile = event.result;
            var globalEvent:customEvent = new customEvent("COMPLETE");
            dispatchEvent(globalEvent);
        }

        private function erreurChargement(event:FaultEvent):void {
            //trace("Erreur lors du téléchargement du fichier : " + _nameFile);
        }
    }
}


ZoneRecherche.mxml
CODE
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="214" height="634" xmlns:menu="*" creationComplete="initialisation();" backgroundColor="#FFFFFF">
    <mx:Script>
        <![CDATA[
            import mx.containers.VBox;
            import mx.controls.Label;
            import mx.core.UIComponent;
        
            include "commun.as"
            
            private var ombreZoneRecherche:DropShadowFilter = new DropShadowFilter();
            private var intituleApplication:Label = new Label();
            private var cartouche1:menuDeroulant = new menuDeroulant();
            private var cartouche2:menuDeroulant = new menuDeroulant();
            private var cartouche3:menuDeroulant = new menuDeroulant();
            private var fondTitreGlobal:UIComponent = new UIComponent();
            private var listeMenu:VBox = new VBox();
            
            private function initialisation():void {
                
                matrice.createGradientBox(214, 27, -Math.PI/2, 0, 0);
                fondTitreGlobal.graphics.beginGradientFill(fillType, colors, alphas, ratios, matrice, spreadMethod);
                fondTitreGlobal.graphics.drawRect(0, 0, 217, 27);
                fondTitreGlobal.graphics.endFill();
                this.addChild(fondTitreGlobal);    
                this.addChild(intituleApplication);        
                
                this.addChild(listeMenu    );
                
                cartouche1._titre = "Par agence";
                cartouche1._pointille = false;
                cartouche1._nbresultat = nbAgence;
                listeMenu.addChild(cartouche1);
                
                cartouche2._titre = "Par poste";
                cartouche2._pointille = true;
                listeMenu.addChild(cartouche2);
                
                cartouche3._titre = "Par secteur";
                cartouche3._pointille = true;
                listeMenu.addChild(cartouche3);
                
                ombreZoneRecherche = new DropShadowFilter(15, 0, 0x333333, 0.5, 10, 0, 0.5, 1, false, false);
                
                positionElement();
            }
            
            private function positionElement():void {
                listeMenu.y = 125;
                intituleApplication.x = 5;
                intituleApplication.y = -1;
                intituleApplication.htmlText = communTxtApplication;
                this.filters = [ombreZoneRecherche];
            }
            
        ]]>
    </mx:Script>
</mx:Canvas>
ArAgorrn
Tout est là normalement

ArA
Fusion
En fait mon exemple était censé montrer comment passer un paramètre d'un composant à un autre. J'aurais dû le préciser, et je vois que ça n'avait pas l'air clair icon_redface.gif
VinceDDH
CITATION(ArAgorrn @ Jun 12 2007, 03:56 PM) *


J'étais déjà tomber sur cette page, mais je ne comprends pas comment l'utiliser...
J'ai crée une class MainClass.as et je fais un import, mais après...
Fusion
A vue de nez, tu devrais remplacer :

CODE
      <menu:zoneRecherche/>


par

CODE
      <menu:ZoneRecherche/>


Sans le "commun.as" je ne peux pas aller plus loin.
VinceDDH
En lisant les commentaires de la page en lien de celui que tu m'as donné Voir ici.

N'y a-t-il pas une solution plus propre pour retrouver ces valeurs ?
VinceDDH
CITATION(Fusion @ Jun 12 2007, 04:22 PM) *
A vue de nez, tu devrais remplacer :

CODE
      <menu:zoneRecherche/>


par

CODE
      <menu:ZoneRecherche/>


Sans le "commun.as" je ne peux pas aller plus loin.


Autant pour moi lors du copier/coller, le nom de mon fichie est bien zoneRecherche.mxml (dans "z" majuscule).

le fichier commun.as ne comprend que des valeurs statiques communes qui sont utilisés sur toute mon application :
commun.as
CODE
// ActionScript file
    
    private var communTxtApplication:String = new String("<font color='#FFFFFF' size='17'>TBWA\\ talent connexions</font>");

    private var matrice:Matrix = new Matrix();
    
    private var fillType:String = GradientType.LINEAR;
    private var colors:Array = [0xA10404, 0xCD0404];
    private var alphas:Array = [100, 100];
    private var ratios:Array = [0x00, 0xFF];
    
    private var spreadMethod:String = SpreadMethod.PAD;

Fusion
Ton type menuDeroulant c'est quoi?
VinceDDH
Tout est là ... :

menuDeroulant.mxml
CODE
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="214" height="125" xmlns:option="*" backgroundColor="#ECECEC" creationComplete="initialisation();" xmlns="*" verticalScrollPolicy="off">
    <mx:Script>
        <![CDATA[
            import mx.containers.VBox;
        
            include "commun.as"
        
            import mx.controls.Label;
            import mx.core.UIComponent;
            import flash.geom.*
            import flash.display.*
            import mx.controls.Image;
            
            public var _pointille:Boolean = new Boolean();    
            public var _titre:String = new String();
            public var _nbresultat:Number = new Number();
        
            private var puce:Image = new Image();
            private var titreMenu:Label = new Label();
            private var lignePointille:UIComponent = new UIComponent();
            private var carrePointille:UIComponent = new UIComponent();
            private var dbFondTitre:UIComponent = new UIComponent();
            private var fondTitre:UIComponent = new UIComponent();
            private var ligneTitre:UIComponent = new UIComponent();
            
            private var conteneurOptions:VBox = new VBox();
            
            [Embed(source='img/puceMenuDeroulant.png')] private var puceTitre:Class;
        
            private function initialisation():void {
                
                dbFondTitre.graphics.beginFill(0xE78787, 1);
                dbFondTitre.graphics.drawRect(0, 0, 214, 22);
                dbFondTitre.graphics.endFill();
                this.addChild(dbFondTitre);
                
                matrice.createGradientBox(20, 20, 0, 0, 0);
                fondTitre.graphics.beginGradientFill(fillType, colors, alphas, ratios, matrice, spreadMethod);
                fondTitre.graphics.drawRect(0, 0, 20, 212);
                fondTitre.graphics.endFill();
                this.addChild(fondTitre);    
                
                if (_pointille) {
                    createPointLine(1, 1, 3, 0);
                    this.addChild(lignePointille);
                }
                
                ligneTitre.graphics.beginFill(0xA5A5A5, 1);
                ligneTitre.graphics.drawRect(0, 0, 214, 1);
                ligneTitre.graphics.endFill();
                this.addChild(ligneTitre);
                
                puce.source = puceTitre;
                this.addChild(puce);
                
                this.addChild(titreMenu);

                this.addChild(conteneurOptions);
                
                for (var i:Number = 0; i < 10; i++) {
                    var option:optionMenu = new optionMenu();
                    option.id = "option" + i;
                    option._valueTxt = "Option" + i;
                    conteneurOptions.addChild(option);                    
                }
                
                positionElement();
                enterData();
                
            }
            
            private function positionElement():void {
                
                dbFondTitre.x = 0;
                dbFondTitre.y = 1;
                
                fondTitre.rotation = -90;
                fondTitre.x = 1;
                fondTitre.y = 22;
                
                titreMenu.x = 21;
                titreMenu.y = 0;
                
                puce.x = 7;
                puce.y = 9;
                ligneTitre.x = 0;
                ligneTitre.y = 23;
                
                conteneurOptions.y = 26;
                conteneurOptions.height = 99;
                conteneurOptions.width = 214;
                
            }
            
            private function enterData():void {
                titreMenu.htmlText = "<font color='#FFFFFF' size='14'>" + _titre + "</font>";
            }
            
            private function createPointLine(largeur:Number, hauteur:Number, espaceLargeur:Number, espaceHauteur:Number):void {
                
                for (var j:Number = 0; j < (this.width / espaceLargeur); j++) {
                    lignePointille.graphics.beginFill(0x404040, 1)
                    lignePointille.graphics.drawRect(j * espaceLargeur, j * espaceHauteur, largeur, hauteur);
                    lignePointille.graphics.endFill();
                }
                
            }
        ]]>
    </mx:Script>
</mx:Canvas>


optionMenu.mxml
CODE
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="197" height="18" creationComplete="createElement();">
    <mx:Script>
        <![CDATA[
            import flash.text.TextFormat;
            import flash.text.TextField;
            import mx.controls.Image;
            import mx.controls.Label;
            import mx.core.UIComponent;
            import mx.effects.Tween;
            import mx.effects.TweenEffect;
            import mx.effects.easing.*;
            
            public var _valueTxt:String = new String();
            
            private var actif:Boolean = new Boolean();
            private var cocheOption:Image = new Image();
            private var libelle:Label = new Label();
            private var formatSelect:TextFormat = new TextFormat();
            private var fondGris:UIComponent = new UIComponent();
            private var globalButton:UIComponent = new UIComponent();
            private var carreRouge:UIComponent = new UIComponent();
            
            
            [Embed(source='img/coche.png')] private var cocheEmbed:Class;
            [Embed(source='img/coche_gris.png')] private var cocheGrisEmbed:Class;
        
            private function createElement():void {
                
                formatSelect.color = 0xFFFFFF;
                
                fondGris.graphics.beginFill(0x737373, 1);
                fondGris.graphics.drawRect(0, 0, 197, 18);
                fondGris.graphics.endFill();
                addChild(fondGris);
                                
                carreRouge.graphics.lineStyle(1, 0xcc0000, 1);
                carreRouge.graphics.drawRect(8, 5, 8, 8);
                addChild(carreRouge);
                
                cocheOption.source = cocheGrisEmbed;
                cocheOption.x = 8;
                cocheOption.y = 4;
                addChild(cocheOption);
                
                libelle.x = 25;
                libelle.y = -1;
                libelle.selectable = false;
                addChild(libelle);
                
                globalButton.graphics.beginFill(0x737373, 0);
                globalButton.graphics.drawRect(0, 0, 197, 18);
                globalButton.graphics.endFill();

                globalButton.addEventListener(MouseEvent.MOUSE_OVER, actionMouse);
                globalButton.addEventListener(MouseEvent.MOUSE_OUT, actionMouse);
                globalButton.addEventListener(MouseEvent.CLICK, actionMouse);
                
                globalButton.buttonMode = true;
                
                addChild(globalButton);

                initialisation();
            }
            
            private function initialisation():void {
                
                actif = false;
                fondGris.alpha = 0;
                cocheOption.alpha = 0;
                libelle.htmlText = _valueTxt;
            }
            
            private function actionMouse(event:MouseEvent):void {
                switch(event.type) {
                     case "mouseOver" :
                         if (actif) { cocheOption.alpha = 0; } else if (!actif) { cocheOption.alpha = 1; }
                     break;
                     case "mouseOut" :
                         if (actif) { cocheOption.alpha = 1; } else if (!actif) { cocheOption.alpha = 0; }
                     break;
                    case "click" :
                        var animationFondOut:Tween;
                        if (actif ) {
                            actif = false;
                            animationFondOut = new Tween(fondGris, fondGris.alpha, 0, 500);
                            animationFondOut.easingFunction = Back.easeIn;
                            animationFondOut.setTweenHandlers(updateAlpha, endClick);
                        } else if (!actif) {
                            actif = true;
                            animationFondOut = new Tween(fondGris, fondGris.alpha, 1, 500);
                            animationFondOut.easingFunction = Back.easeIn;
                            animationFondOut.setTweenHandlers(updateAlpha, endClick);
                        }
                    break;        
                }
            }
            
            private function updateAlpha(val:Object):void { fondGris.alpha = Number(val); }
            private function endClick (val:Object):void {
                if (actif) {
                    cocheOption.source = cocheEmbed;
                    carreRouge.alpha = 0;
                    libelle.htmlText = "<font color='#FFFFFF'>" + _valueTxt + "</font>";
                    libelle.x = 36;
                } else if (!actif) {
                    cocheOption.source = cocheGrisEmbed;
                    carreRouge.alpha = 1;
                    libelle.htmlText = _valueTxt;
                    libelle.x = 25;
                }
            }
            
        ]]>
    </mx:Script>
</mx:Canvas>
Fusion
Essaie ça :

Dans ZoneRecherche

Je crée une fonction appelée setParam
CODE
            
public function setParam(n:Number):void        
{            
        nbAgence = n;                
}





Dans FichierRacine

Je donne un nom à zoneRecherche
CODE
<menu:zoneRecherche id="zonerecherche" />


Après avoir calculé nbAgence je l'envoie à la zoneRecherche
CODE
            private function resultatAgence(event:customEvent):void {
                agenceXML = chargementAgence._contentFile;
                agenceXML.ignoreWhite = true;
                nbAgence = agenceXML.agences.agence.length;
                zonerecherche.setParam(nbAgence);
            }







Et ainsi, zoneRecherche dispose de l'information "nbAgence".
Il doit exister d'autres solutions, je serais curieux de les connaître...

VinceDDH
Merci pour ta réponse Fusion,
Mais j'aimerais aussi comprendre la solution ArAgorrn, ou du moins une solution qui n'a pas besoin de "ré-écrire" les variables dans chaque composants...

J'espère que quelqu'un pourra me (nous) aider !

Merci d'avance !
VinceDDH
Suite à un autre post, on m'a fait découvrir le parentApplication, qui peut servir un peu comme un _global....
Ceci est une version "bas débit" de notre forum. Pour voir la version complète avec plus d'informations, la mise en page et les images, veuillez cliquer ici.
Invision Power Board © 2001-2008 Invision Power Services, Inc.