为什么MXML数据绑定吞下TypeErrors? [英] Why does MXML data binding swallow TypeErrors?

查看:133
本文介绍了为什么MXML数据绑定吞下TypeErrors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码抛出错误#1009:无法访问空对象引用的属性或方法

  var label:Label; 
label.text = value;

但是,如果它位于由MXML数据绑定设置的setter内,则不会: / p>

  public function set buggySetter(value:String):void {
var label:Label;
label.text = value; //将静默失败
}

要重现这个奇怪的行为,首先,创建一个简单的定制组件通过扩展s:标签:

  package {
import spark.components.Label;

public class BuggyLabel extends Label {
public function set buggySetter(value:String):void {
var label:Label;
label.text = value; //将默认失败
}
}
}

Sectond将BuggyLabel添加到应用程序并绑定buggySetter:

 < fx:脚本> 
<![CDATA [
[Bindable]
public var foo:String ='NULL has no properties';
]]>
< / fx:脚本>

< local:BuggyLabel buggySetter ={foo}/>

为什么这个应用程序默默地失败?

解决方案

这个问题的答案实际上是相当短的:它是由Flex SDK工程师做出的架构决策。如果您查看Flex源代码,您将看到一个 try ... catch 阻止吞噬大多数错误在Binding中抛出。



专业版:使您更容易使用绑定,因为您不必考虑所有可能的错误状态。



< con> Con :它可能更难调试(虽然如果你知道这可能会发生,你有良好的单元测试,你可以减少从这方面的沮丧到接近零)






我在谈论的源代码可以在 mx.binding.Binding (in方法 wrapFunctionCall()中的'框架'项目)。以下是相关的部分:

  try {
...
}
catch :错误)
{
//执行srcFunc或destFunc时某些错误正常,
//所以我们吞下它们:
//错误#1006:尝试在对象不是一个函数。
//错误#1009:null没有属性。
//错误#1010:undefined没有属性。
//错误#1055: - 没有属性。
//错误#1069:属性 - 未找到 - 并且没有默认值
//我们允许抛出任何其他错误。
if((error.errorID!= 1006)&&
(error.errorID!= 1009)&&
(error.errorID!= 1010)&
(error.errorID!= 1055)&&
(error.errorID!= 1069))
{
throw error;
}
else
{
if(BindingManager.debugDestinationStrings [destString])
{
trace(Binding:destString =+ destString +, error =+ error)
}
}
}


The following code shoud throw an Error #1009: Cannot access a property or method of a null object reference:

var label:Label;
label.text = value;

However, it doesn't if it's inside of a setter which is set by MXML data binding:

public function set buggySetter(value:String):void {
    var label:Label;
    label.text = value; //will fail silently
}

To reproduce this weird behaviour, first, create a simple custom component by extending s:Label:

package {
    import spark.components.Label;

    public class BuggyLabel extends Label {
        public function set buggySetter(value:String):void {
            var label:Label;
            label.text = value; //will fail silently
        }
    }
}

Sectond, add BuggyLabel to an Application and bind buggySetter:

<fx:Script>
    <![CDATA[
        [Bindable]
        public var foo:String = 'NULL has no properties';
    ]]>
</fx:Script>

<local:BuggyLabel buggySetter="{foo}"/>

Why does this app fail silently?

解决方案

The answer to that question is actually fairly short: it's an architectural decision made by the Flex SDK engineers. If you take a look at the Flex source code, you'll see a try ... catch block swallowing most errors thrown in a Binding.

Pro: makes it easier to use bindings, since you don't have to account for all possible faulty states

Con: it can be harder to debug (though if you know this can happen and you have good unit tests, you can reduce frustration from that side to nearly zero)


The source code I was talking about can be found in mx.binding.Binding (in the 'framework' project) in method wrapFunctionCall(). Here's the relevant part:

    try {
       ...
    }
    catch(error:Error)
    {
        // Certain errors are normal when executing a srcFunc or destFunc,
        // so we swallow them:
        //   Error #1006: Call attempted on an object that is not a function.
        //   Error #1009: null has no properties.
        //   Error #1010: undefined has no properties.
        //   Error #1055: - has no properties.
        //   Error #1069: Property - not found on - and there is no default value
        // We allow any other errors to be thrown.
        if ((error.errorID != 1006) &&
            (error.errorID != 1009) &&
            (error.errorID != 1010) &&
            (error.errorID != 1055) &&
            (error.errorID != 1069))
        {
            throw error;
        }
        else
        {
            if (BindingManager.debugDestinationStrings[destString])
            {
                trace("Binding: destString = " + destString + ", error = " + error);
            }
        }
    }

这篇关于为什么MXML数据绑定吞下TypeErrors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆