Flex+MXML id 属性命名约定/最佳实践
由于命名空间内存在太多潜在的污染,特别是解析器自动声明具有 id 的任何 MXML 组件可以使用该 ID 公开访问,因此我发现仅使用驼峰命名法作为 MXML 元素 id 是很危险的。
例如:
<mx:TextInput id="textOne" text="This is a test" />
<mx:Button id="buttonOne" label="Click Me" click="textOne.text='Testing 1-2-3'" />
在这个相当简单的示例中,很明显按钮的单击处理程序中的“textOne”引用了 id 为“textOne”的 TextInput。在下面的示例中,这一点变得不太明显:
<mx:Button id="buttonTwo" label="Change Tab" click="tabNav.selectedChild=newState" />
这里一点也不明显,“newState”指的是 ID 为“tabNav”的 TabNavigator 上下文中 ID 为“newState”的 INavigationContent 实例。
如果我们在 fx:Script 块中执行此操作,或者在外部 AS 类文件中执行此操作,情况会变得更糟(更好)。
public function buttonClickHandler(event:FlexEvent):void {
tabNav.selectedChild = newState;
}
在这个 AS 类文件中,我们没有声明 tabNav 或 newState,因此,乍一看,我们希望解析器会抱怨。但事实并非如此,因为这些 id 被自动声明为公共属性。
我的意思是,我感觉应该制定一个约定,使这些公共属性的来源更加明显。类似
id="mxTabNav"
或
id="TXTMyTextInput"
可能只是使用下划线
id="my_text_input"
在 Flash 开发中,我们很多人都为(自动)声明的阶段实例(例如“mcMyTabBar”或“navMC”或“playPauseBTN”)执行此操作。
我正在寻找 Flex 社区对此的意见。我只是想太多了吗?我读过的有关该主题的所有相关样式指南和最佳实践文档都只是说“对 ids 使用驼峰命名法,并确保 id 属性是第一个属性”。
你怎么看?
Since there's so much potential pollution within namespaces, particularly with the parser automatically declaring any MXML component with an id to be publicly-accessible using that ID I'm finding it dangerous to use just camelCase for MXML element ids.
eg:
<mx:TextInput id="textOne" text="This is a test" />
<mx:Button id="buttonOne" label="Click Me" click="textOne.text='Testing 1-2-3'" />
In this rather trivial example, it's pretty clear that "textOne" in the Button's click handler is referring to the TextInput with an id of "textOne". It becomes less obvious in the following example:
<mx:Button id="buttonTwo" label="Change Tab" click="tabNav.selectedChild=newState" />
It's not at all obvious here that "newState" is referring to an INavigationContent instance with an id of "newState" within a TabNavigator context with an id of "tabNav".
This would become even less clear if we were doing this within an fx:Script block, or worse (better) within an external AS class file.
public function buttonClickHandler(event:FlexEvent):void {
tabNav.selectedChild = newState;
}
Nowhere in this AS class file have we declared tabNav or newState, and so, at first glance, we expect the parser to complain. But it doesn't because these ids are automagically declared as public properties.
What I'm getting at is it feels to me that we should develop a convention that makes it more obvious where these public properties are coming from. Something like
id="mxTabNav"
or
id="TXTMyTextInput"
or maybe just use underscores
id="my_text_input"
In Flash development, a lot of us did this for (automatically) declared stage instances like "mcMyTabBar" or "navMC" or "playPauseBTN".
I'm looking for input from the Flex community about this. Am I just overthinking things? All of the related Style Guides and best practices documents I've read on the topic just say "use camelCase for ids, and make sure the id property is the first property".
What's your take?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的看法是,这并不是什么大不了的事。
我的所有组件都被封装(尽可能多),我什至不给 MXML 中的元素提供 ID,除非我真的必须这样做(即模块后面的代码中的某些内容正在引用它)。
我宁愿让一个类通过
函数在 myFooComponent 上设置文本
来与我的组件交互,而不是
如果您根本不引用 MXML 文件,您可以更进一步。我们有用 AS 编写的 View 类,它们都扩展了 Group。在他们的 createChildren 函数中,我们添加了用 MXML 文件编写的组件。
这意味着没有任何东西直接针对 MXML 文件(除非它们开始通过显示列表进行搜索,这会让人感到很紧张),并且 View 类完全封装了 View 对象。
我们甚至更进一步,只通过接口引用视图类,因此我们可以非常轻松地实现视图的多个实现(例如针对客户特定的更改)。
My take is that it's not really that big a deal.
All my components are encapsulated (as much as possible), I don't even give IDs to elements in MXML unless I really have to (i.e. something in the code behind module is referencing it).
I'd rather a class interacted with my component via
where the function sets the text on the myFooComponent
rather than
You can take this a step further if you don't reference the MXML file at all. We have View classes written in AS which all extend Group. In their createChildren functions, we add the component written in the MXML file.
This means nothing is targeting the MXML file directly (unless they start rooting through the display list, which gets a swift rap on the knuckles), and the View Class encapsulates the View object completely.
We even take this a step further and only ever reference View Classes by their Interfaces, so we can have multiple implementations of a view (say for customer specific changes) very easily.
我建议使用“目的和类名”作为 id“”。例如:
对于事件处理程序,使用“on”模式。例如:
I recommend to use couple "purpose and class name" for id "". For example:
and for event handlers use "on" pattern. For example: