有什么方法可以将 valueCommit 生命周期添加到 Actionscript 中的非 mxml 组件吗?

发布于 2024-09-08 12:23:13 字数 133 浏览 5 评论 0原文

根据我的经验,mxml 组件使用的 invalidate/commitProperties 模型非常有用,我希望能够在我的 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 lifecycle events like that to my objects? Is there a global object lifecycle manager?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-09-15 12:23:13

正如 Robert Bak 所指出的,您基本上需要自己为非 UI 组件实现这样的机制。

我发现这是在模型类上使用的一种非常有用的技术,因为当您的模型类不是简单的数据传输对象(即它们具有任何类型的多属性逻辑)时,它可以显着减少绑定属性更新的“颠簸”封装在其中。

由于我的用例是模型对象,因此我不需要 IInvalidating 的所有方法。

这是我的具体实现,作为您自己努力的起点。请注意,这来自我们使用的名为 RAFModel 的“基本模型类”,并且这是针对 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();
        }
    }

}

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();
        }
    }

}
情深如许 2024-09-15 12:23:13

Invalidation 和 commitProperties 未链接到 MXML(您可以将其用作组件),但它链接到 Flex 管理的视觉组件生命周期(因为它们是唯一的生命周期)需要与Flash逐帧渲染同步)。因此,除非您谈论的是视觉组件,否则它不会开箱即用。

但是,如果您希望为非可视类实现相同的机制,您可能应该从实现 IInvalidating (docs)并创建一种机制,在需要完成验证时调用 validateNow() 函数。

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.

居里长安 2024-09-15 12:23:13

Flex 组件生命周期旨在处理用户界面组件的创建、销毁以及其间的更改。我个人认为这种方法不适合非用户界面组件。

如果需要,您可以在域模型对象中扩展 UIComponent,然后将该域模型作为子级添加到容器中。然后它将经历 Flex 组件生命周期验证阶段(commitProperties、updateDisplayList 和measure)。

但是,我不推荐这种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文