1

I'm new to Action-script OOP and i need to know how to chain methods like this example i have

I.$(button).bind('click',clickButton).bind('rollover',overButton).bind('rollout',outButton)

First i need to remove the I. to use dollar sign only like jQuery :) to select MovieClip and apply any action on it second issue that i have because this way i'm using static Methods Action-script restrict's me to use only static property saving the last one who called the action here is the class code to know what i mean:

package com.MAIN
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class I extends Sprite
    {
        private static var cSelector:Sprite;

        public static function $(selector:Sprite)
        {
            cSelector = selector
            return I;
        }

        public static function alpha(val:Number)
        {
            cSelector.alpha = val;
            return I;
        }

        // bind mouse event to the element
        public static function bind(EventStr,func:Function)
        {
            var func1:Function = function(e:MouseEvent){
                func(cSelector);
            }
            // select the event from the list
            if(typeof(EventStr) == 'string'){
                // map the events in lowercase
                var events:Object = {click:'CLICK',rollover:'ROLL_OVER',rollout:'ROLL_OUT',dblclick:'DOUBLE_CLICK',mousedown:'MOUSE_DOWN',mousemove:'MOUSE_MOVE',mouseout:'MOUSE_OUT',mouseover:'MOUSE_OVER',mouseup:'MOUSE_UP',mousewheel:'MOUSE_WHEEL'};
                // check if the event exists in the list
                if(events[EventStr] && MouseEvent[events[EventStr]]){
                    cSelector.addEventListener(MouseEvent[events[EventStr]],func1);
                }
            }else if(typeof(EventStr) == 'object'){
                // add the event
                cSelector.addEventListener(EventStr,func1);
            }
            return I;
        }

        public static function remove()
        {
            cSelector.parent.removeChild(cSelector);
            return I;
        }

    }
}
5
  • 1
    Static proerties and methods are bad practice in an object-oriented language. To create a flent API, each method needs to return an object of the same type, that has the method. Check out the open-source Mockalate project for an example. Commented Mar 5, 2013 at 16:16
  • I know it's bad practice put i need to know if there is any workaround to fix this two situations that i have ? Commented Mar 5, 2013 at 16:22
  • It's not a good idea to try to shoehorn the language you're using into the metaphor you're used to (especially when large parts of that metaphor stem from the weaknesses, not the strengths, of that language). But if you can figure out how to use $ as a package or Class name, it will work as a namespace, which is how this works in JS. Alternatively, you can set your variable $ to a package or class. Commented Mar 5, 2013 at 16:26
  • Why call your class "I" why not just call it "JQuery". This should not be under the OOP tag. There are so many bad practices here I wouldn't even know where to start. I do hope this is not being implemented in a production environment. Commented Mar 5, 2013 at 16:34
  • I just need an example to use it like jQuery or dollar sign : $(movieclip).alpha(0.5).bind('click',function()....) Commented Mar 5, 2013 at 17:16

2 Answers 2

2

Here you go, some steps in the right direction. However, this is a really, really, really crappy idea.

//$.as
package
{
    import flash.display.DisplayObject;

    //NOTE: there's NO class definition

    public function $( selector : DisplayObject ) : IDisplayObject
    {
        //traverse displaylist to find <code>selector</code>
        //and return an instance of IDisplayObject that holds the reference        
    }
}

//IDisplayObject.as
package
{
    public interface IDisplayObject{
        function alpha( value : Number ) : IBinding;
    }
}

//IBinding.as
package jquery
{
    public interface IBinding{
        function bind( eventName : String, callback : Function, ...parameters ):void;
    }
}

Once you've created concrete implementations of these you can do:

$( someMC ).alpha( .5 ).bind( 'click', function(){ trace( 'what a miraculously crappy idea !!!!' ) } );
Sign up to request clarification or add additional context in comments.

Comments

1

You could try it like this:

interface Test {
  function doBla(): Test
  function moreBla(): Test
}

public class StaticTest {
  private static const instance: Test = new InternalTest()

  public static doBla() : Test {
    return instance.doBla();
  }

  public static moreBla() : Test {
    return instance.moreBla();
  }
}

internal class InternalTest implements Test {
  function doBla(): Test {
    trace("bla");
    return this;
  }

  function moreBla(): Test {
    trace("more bla");
    return this;
  }
}

2 Comments

I like your example put to use it i have to write it this way : StaticTest.doBla() I need it like this StaticTest(selector).doBla()
StaticTest(selector) is not possible with classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.