6 Replies Latest reply on Jan 12, 2010 8:03 AM by tfcenturion

    StyleManager.getStyleDeclaration in a static function

    tfcenturion

      Hi Everyone,

       

      I have been watching these 360|Flex conference videos these last few days and i have tryed using some of the code found in them to varing success.

       

      One code example i am trying to get to work, is from Ben Clinkinbeard on creating reusable components.

      The code reads like:

      private static var stylesInited:Boolean = initStyles();
                
      private static function initStyles():Boolean
      {
           // check for existence of style declaration
           var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration("LoginForm");
           // if one does not exist create it
           if(!selector)
           {
                selector = new CSSStyleDeclaration();
           }
           // set default style properties - don't use setStyle()!
           selector.defaultFactory = function():void
           {
                // built-in styles
                this.backgroundAlpha = 1;
                this.backgroundColor = 0xDEDEDE;
                this.paddingTop = this.paddingRight = this.paddingBottom = this.paddingLeft = 10;
                
                // custom style
                this.inputColor = 0xFFFFFF;
           }
           // apply defined styles
           StyleManager.setStyleDeclaration("LoginForm", selector, false);
           
           return true;
      }
      

       

      My question is this, can you or is there a way i can place the styleName passed to this instead of hard coding a selector id.

      eg:

      var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration(this.styleName);

       

       

      I have tryed the above but i get a complier error about you cant use this keyword in a static function.

       

      I do think i need to have this as dynamic as i would like to createa a component that would have several instances of it on the stage with differant styles.

       

      If anyone could help on this or point me in the right direction i would be greatfull.

       

      Paul

        • 1. Re: StyleManager.getStyleDeclaration in a static function
          RUSH-ME Level 3

          Hi

           

          I think you can use the variable styleName in StyleManager.getStyleDeclaration();

           

          As we can't use this keyword in static function so its giving error. you can pass a variable like this.

           

          StyleManager.getStyleDeclaration(styleSelector);

           

          where "styleSelector" is the variable holding the style name.

           

          Hope this helps

           

          Rush-me

          • 2. Re: StyleManager.getStyleDeclaration in a static function
            tfcenturion Level 1

            Hi RUSH-ME,

             

            No that didnt work, i also tryed a function that just returned the selector as a string but to no avail there either.

             

            I get the error 'Access of undefined property' or 'Access of undefined function'.

             

            Any more thoughts..

             

            TFC

            • 3. Re: StyleManager.getStyleDeclaration in a static function
              RUSH-ME Level 3

              Hi

               

              Can you post your code updated code.

               

              I have done in that way and its working fine.

               

              Warm Regards

              Rush-me

              • 4. Re: StyleManager.getStyleDeclaration in a static function
                tfcenturion Level 1

                Hi RUSH-ME,

                 

                Sure, the code i posted fisrt was from Ben's demo but mines basic same.

                package{.... blah blah
                
                protected var _styleName:String;
                private static var stylesInited:Boolean = initStyles();
                
                public function ImageButton()
                {
                     super();
                     
                     _styleName = String(this.styleName);
                }
                
                
                private static function initStyles():Boolean
                {
                     trace("_styleName: ", _styleName);
                        // Check for existence of style declaration. //
                     var styles:CSSStyleDeclaration = StyleManager.getStyleDeclaration( "." + _styleName );
                     
                     // if one does not exist create it
                     if(!styles)
                     { styles = new CSSStyleDeclaration("styles"); }
                     
                     // Create All the default Styles we need. //
                     styles.defaultFactory = function():void
                     {
                          trace("initStyles(): Applying Custom Styles.");
                          // Built-In Styles. //
                              
                          // Custom Styles. //
                          this.imagePadding = 15;     
                     }
                     
                     // Apply Defined Styles. //
                     StyleManager.setStyleDeclaration("." + _styleName, styles, false);
                          
                     //this.setStyle("imagePadding", 40);
                     trace("initStyles():", styles.getStyle( "imagePadding" ));
                     
                     return true;
                }
                

                 

                If i got the wrong end of the stick, could you post a small example/code to show the way.

                 

                Thank you  on this.

                 

                TFC

                • 5. Re: StyleManager.getStyleDeclaration in a static function
                  RUSH-ME Level 3

                  Hi

                   

                  public

                   

                   

                   

                   

                   

                  static function createStyle(styleSelector:String):CSSStyleDeclaration

                  {

                   

                  var style:CSSStyleDeclaration = StyleManager.getStyleDeclaration(styleSelector);

                   

                  if (!style)

                  {

                  style =

                  new CSSStyleDeclaration();

                  StyleManager.setStyleDeclaration(styleSelector, style,

                  false);

                  }

                   

                  return style;

                  }

                   

                  You can pass static variable to this function and create a style.

                   

                  Hope this helps.

                   

                  Rush-me

                  1 person found this helpful
                  • 6. Re: StyleManager.getStyleDeclaration in a static function
                    tfcenturion Level 1

                    Hi there RUSH-ME,

                     

                    Cheers for clearing that one, yes once i switched it to a function parameter it passed through ok.

                     

                    Thank you .

                     

                     

                    TFC