有人可以向我解释一下这个常见的绑定陷阱吗? (使用错误的可绑定事件名称)
我参考了这个网站链接文本
在事件名称中使用了错误的事件名称 [Bindable] 标签可以使您 申请不绑定您的财产, 你甚至不知道为什么。什么时候 您将 [Bindable] 标签与 自定义名称,下面的示例看起来 喜欢一个好主意:
public static const EVENT_CHANGED_CONST:String = "eventChangedConst";
private var _number:Number = 0;
[Bindable(event=EVENT_CHANGED_CONST)]
public function get number():Number
{
return _number;
}
public function set number(value:Number) : void
{
_number = value;
dispatchEvent(new Event(EVENT_CHANGED_CONST));
}
上面的代码分配了一个静态 属性到事件名称,然后 使用相同的分配来调度 事件。然而,当值 更改后,绑定不会出现 去工作。原因是该事件 名称将为 EVENT_CHANGED_CONST 并且 不是变量的值。
代码应该写成 如下:
public static const EVENT_CHANGED_CONST:String = "eventChangedConst";
private var _number:Number = 0;
[Bindable(event="eventChangedConst")]
public function get number():Number
{
return _number;
}
public function set number(value:Number) : void
{
_number = value;
dispatchEvent(new Event(EVENT_CHANGED_CONST));
}
我同意,错误的例子看起来确实是个好主意,我会这样做,因为我认为这是正确的方法,并且避免了打字错误的可能性。为什么使用常量的名称而不是它的值?这肯定是不对的吧?
我很欣赏你的见解
I refer to this site link text
Using the wrong event name in the
[Bindable] tag can cause your
application to not bind your property,
and you will not even know why. When
you use the [Bindable] tag with a
custom name, the example below looks
like a good idea:
public static const EVENT_CHANGED_CONST:String = "eventChangedConst";
private var _number:Number = 0;
[Bindable(event=EVENT_CHANGED_CONST)]
public function get number():Number
{
return _number;
}
public function set number(value:Number) : void
{
_number = value;
dispatchEvent(new Event(EVENT_CHANGED_CONST));
}
The code above assigns a static
property to the event name, and then
uses the same assignment to dispatch
the event. However, when the value
changes, the binding does not appear
to work. The reason is that the event
name will be EVENT_CHANGED_CONST and
not the value of the variable.The code should have been written as
follows:
public static const EVENT_CHANGED_CONST:String = "eventChangedConst";
private var _number:Number = 0;
[Bindable(event="eventChangedConst")]
public function get number():Number
{
return _number;
}
public function set number(value:Number) : void
{
_number = value;
dispatchEvent(new Event(EVENT_CHANGED_CONST));
}
I agree, the wrong example does look like a good idea and I would do it that way because I think it's the right way and avoids the possibility of a typing error. Why is the name of the constant used instead of it's value? Surely this can't be right?
I appreciate your insights
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为标准 Flex 编译器有时并不那么聪明...而且我感受到你的痛苦!我已经多次抱怨过这个问题。
如果我没记错的话,这是因为编译器执行了多次传递。早期阶段之一将元数据更改为 AS 代码。此时,编译器尚未解析 AS 代码的其余部分,因此它无法解析常量或对其他文件中静态变量的引用。
我唯一可以建议的是注册 Adobe JIRA,为该错误投票,并希望 4.5 中的编译器修复能带来一些缓解。
Because the standard Flex compiler isn't that clever at times... and I feel your pain! I've complained about this exact problem more than a few times.
If I remember correctly, it's because the compiler does multiple passes. One of the early passes changes the Metadata into AS code. At this point in the compiler it hasn't parsed the rest of the AS code, so its not capable of parsing Constants or references to static variables in other files.
The only thing I can suggest is sign up to the Adobe JIRA, vote for the bug, and hope that the compiler fixes in 4.5 bring some relief.