问题描述
好吧,Flex 中的模块很受欢迎,但我不知道为什么关于 Flex 模块不同用途的文档和示例如此稀缺.
Ok, modules in Flex are popular but I have no idea why documentation and examples on the different uses of Flex modules can be so scarce.
无论如何,对于这个问题,我将以经典的员工/部门为例.我有一个包含 mx:TabNavigator 的 main.mxml.每个选项卡都由一个 s:ModuleLoader 加载.
Anyway, for this question, I will take the classic Employee/Department example. I have a main.mxml that contains an mx:TabNavigator. Each tab is loaded by an s:ModuleLoader.
表:员工{empID,empName,deptID},部门{deptID,deptName}
Tables: Employees {empID,empName,deptID}, Deparments {deptID,deptName}
Tab Navigator 仅包含一个名为 Employee 的选项卡(在我们的示例中).我有一个 Employee.mxml 模块.在该模块中,我有一个填充有员工详细信息的数据网格.我使用 getEmployees($deptID) 函数.正如您可能猜到的那样,此函数返回一组在特定部门工作的员工.
The Tab Navigator contains only one tab (for our example) called Employee. I have an Employee.mxml module. In that module, I have a datagrid that is populated with Employee details. I use the getEmployees($deptID) function. This function, as you may guess, returns me an array of Employees who work in a particular department.
在 TabNavigator 之外,我有一个由 Department.deptName 填充的 DepartmentDropDownList.
Outside the TabNavigator, I have a departmentDropDownList that is populated with departments.deptName.
我的目标是在我从 DropDownList 中选择特定部门时加载 Employee 模块.我有一个 DropDownList 的 changeHandler 可以给我 deptID.
My objective is to load the Employee module when I select a particular department from the DropDownList. I have a changeHandler for the DropDownList that can give me the deptID.
protected function departmentDropDownList_changeHandler(event:IndexChangeEvent):void
{
MyDeptID=departmentDropDownList.selectedItem.deptID;
//var ichild:*=employeeModule.child as IModuleInfo;
}
现在,百万美元的问题是:如何将这个部门 ID 传递给员工模块.后者有一个 employee_creationCompleteHandler 调用 getEmployees(deptID):
Now, the million dollar question is: How do I pass this deptID to the Employees module. The latter has an employee_creationCompleteHandler that calls getEmployees(deptID):
protected function EmployeesDg_creationCompleteHandler(event:FlexEvent):void
// I only need to get the deptID from the departmentDropDownList outside the Employee module.
// If I could create a global variable deptID, that would be great!
getEmployeessResult.token=employeeService.getEmployeess(deptID);
}
我曾尝试使用 [Bindable] 变量,但没有成功.
I have attempted to use [Bindable] variables but without success.
非常感谢您的建议.
推荐答案
你不能真正保证在 creationComplete 运行时会设置 deptID——听起来你在等待服务器结果——所以这是可能不是最好的处理方式.
You can't really guarantee that the deptID will be set when creationComplete runs--it sounds like you're waiting for a server result--so this is probably not the best way to handle it.
你需要注意的一件事是直接从主应用程序引用完整的模块类,因为模块的重点是你不应该在模块类中编译到主类中(以减少文件大小/加载时间).
One of the things you need to be careful of is directly referencing the full Module Class from the main Application, because the point of modules is that you should not compile in the module Class into the main Class (to reduce file size/load times).
所以你可能想做的是创建一个接口.这在主应用程序和模块之间创建了一个契约",而不携带所有的实现代码.可能看起来像这样
So what you might want to do is create an Interface. This creates a "contract" between the main application and the Module without carrying all the implementation code with it. That might look something like this
public interface IEmployeeModule {
function set deptID(value:int):void;
}
然后,你的模块可能有类似这样的代码:
Then, your Module might have code that's something like this:
protected var _deptID:int;
public function set deptID(value:int):void {
_deptID = value;
var token:AsyncToken=employeeService.getEmployeess(deptID);
token.deptID = value;//in case department id changes, you can determine if you still care
}
请注意,虽然在您的项目很小的时候使用全局变量似乎是一个很棒的想法,但它们是一个非常不好养成的习惯.修复一个项目几乎是不可能的,这个项目从这些开始,然后发展到没有人能够准确地弄清楚在成百上千个可以访问变量的类中哪些正在以错误的方式改变它.时间不对.
Note that, though global variables seem like a wondermous idea when your project is small, they are a very bad habit to get into. It can be almost impossible to repair a project that starts out with these and then grows to the point that no one can figure out exactly which of the hundreds or thousands of Classes that have access to a variable are changing it in the wrong way at the wrong time.
您尤其不想在模块中使用全局变量,因为当模块开始为定义争吵时,它们会导致非常严重的问题.
You ESPECIALLY don't want to use global variables with Modules, as they can cause really bad problems when the modules start fighting over the definition.
这篇关于Flex 模块和应用程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!