Robotlegs AS3: Mediating A View (like TextField) Without A Special Class

When I started using Robotlegs some days ago I was wondering how I could mediate just a single TextField without providing a new class for mapping.

The best-practises document from the MVC framework Robotlegs says that you have to use the mapView method to bind a mediator class to a view class. The signature goes like this:

mapView(viewClassOrName:*, mediatorClass:Class, injectViewAs:Class = null, autoCreate:Boolean = true, autoRemove:Boolean = true):void

And the description is like:

mapView accepts a view class, MyAwesomeWidget, or a fully qualified class name for a view, com.me.app.view.components::MyAwesomeWidget as the first parameter.

That made me believe I could only map the class “TextField” to a mediator. But that would create a new mediator everytime I would add a TextField to the stage. The description is misleading in that you can also put in a reference to a new created view instance instead of only a view class or fully qualified class name.

In short you can do this:

override public function startup():void
		{
			tf = new TextField();
			mediatorMap.mapView(tf, TextfieldMediator, TextField);
			contextView.addChild(tf);
		}

Like you can see from the comments it is possible but not in the way I described above!

Dieser Beitrag wurde unter GameDev, HowTo, Programming, Release abgelegt und mit , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

4 Antworten auf Robotlegs AS3: Mediating A View (like TextField) Without A Special Class

  1. shaun sagt:

    Unfortunately, that will still map your mediator to all TextFields – the first parameter to mapView will be translated into the instance’s fully qualified class name internally anyway. Bear in mind that it’s not usually a good idea to Mediate at that level – it would probably make more sense to mediate the form/component that the TextField is sitting in.

  2. Mark sagt:

    You are right. Must have been tired when I was testing this. Thanks for bringing light into this. So you say its not possible to mediate a single DisplayObject (Sprite, TextField, etc). One must create a new class every time?

  3. Joel Hooks sagt:

    The docs are called “best practices” and aren’t meant to be exhaustive in terms of edge cases or even all of the features of Robotlegs. That said, you can easily mediate a single instance of a TextField. I wouldn’t recommend it. Either mediate its container or an extension of the TextField for MyAwesomeTextField. But the following will mediate one and only one TF:

    //map it with no automatic mediation
    mediatorMap.mapView(TextField, TextFieldMediator, TextField, false);

    //later
    var myTextFieldInstance:TextField = new TextField();
    mediatorMap.createMediator(myTextFieldInstance);

    There you go, a single mediated TextField in an application. The false at the end of the mapping statement is turning off automatic mediation.

    Automatic mediation is a convenient implementation detail. It is great for 98% of use cases (statistic totally just made up) and provides an easy way to mediate your classes without writing much code. The framework has to have some way to differentiate your classes to assign them mediators, which is why the general practice is to simple extend the base class (especially in regards to low level common use DisplayObjects).

  4. Mark sagt:

    Thank you for that.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">