开发者

Is there any way to add a valueCommit lifecycle to non-mxml components in Actionscript?

The invalidate/commitProperties model used by mxml components is very useful, in my experience, and I'd like to be able to make use of it in domain model objects in my actionscript applications. How can I go about adding 开发者_Python百科lifecycle events like that to my objects? Is there a global object lifecycle manager?


As noted by Robert Bak, you're essentially on your own to implement such a mechanism for non-UI components.

I've found this a very useful technique to use on model classes, since it can dramatically reduce the "thrashing" of bound-property updates when your model classes are not simple data transfer objects - i.e. they have any kind of multi-property logic encapsulated within them.

Since my use-case is for model objects, I didn't need all the methods of IInvalidating.

Here's my particular implementation as a starting point for your own efforts. Note that this comes from a "base model class" we use called RAFModel and that this is for the Flex 4 SDK.

// INVALIDATION AND COMMITPROPERTIES PATTERN

private var invalidatePropertiesFlag:Boolean;

public function invalidateProperties():void
{
    if (!invalidatePropertiesFlag)
    {
        invalidatePropertiesFlag = true;
        invalidateModelObject(this);
    }
}

protected function commitProperties():void
{
   // override this
}

// -- INVALIDATION SUPPORT
public static var invalidObjects:Dictionary = new Dictionary(true);
public static var validatePending:Boolean = false;

public static function invalidateModelObject(obj:RAFModel):void
{
    invalidObjects[obj] = true;

    if (!validatePending)
    {
        validatePending = true;
        FlexGlobals.topLevelApplication.callLater(validateObjects);
    }
}

protected static function validateObjects():void
{
    var invalidQueue:Dictionary = invalidObjects;

    // start a fresh tracker for further invalidations
    // that are a side effect of this pass
    invalidObjects = new Dictionary(true);
    // ready to receive another call
    validatePending = false;

    for (var o:* in invalidQueue)
    {
        var rm:RAFModel = o as RAFModel;
        if (rm)
        {
            // clear the flag first, in case we're reentrant
            // on any given instance
            rm.invalidatePropertiesFlag = false;
            rm.commitProperties();
        }
    }

}


Invalidation and commitProperties isn't linked to MXML (you can use it with as components) but it is linked to the flex managed visual component lifecycle (as they are the only ones which need to be synchronized with the flash frame by frame rendering). So unless you're talking about visual components it will not work out of the box.

But if you're looking to implement the same mechanism for your non-visual classes, you should probably start by implementing IInvalidating (docs) and creating a mechanism that calls the validateNow() function when the validation needs to be done.


The Flex Component LifeCycle is designed to handle a User Interface Component's creation, destruction, and changes during the time in between. I, personally, do not find the approach appropriate for non-User Interface components.

You could, if you wanted, extend UIComponent in your domain model objects and then add that domain model as a child to a container. it would then go through the Flex Component LifeCycle validation phases (commitProperties, updateDisplayList, and measure).

But, I would not recommend that approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜