当我开始时,我发现 mxml 和 AS3 与 Zend 框架、php 和 WAMP 设置的混合有点令人生畏,并且很难将 Flex 用作编程环境。全局变量的使用稍微减轻了这种痛苦。
对我有用的方式......虽然我确信可能有更好的例子,但以下是;
首先创建一个全局类
package
{
public class Globals
{
[Bindable] public var name : String;
[Bindable] public var email : String;
[Bindable] public var lecturersResult : CallResponder;
[Bindable] public var studentsResult : CallResponder;
[Bindable] public var datagrid_lecturers : DataGrid;
[Bindable] public var dropDownList_lecturerid : DropDownList;
[Bindable] public var DatabaseTables : Accordion;
[Bindable] public var lectAdd : Button;
// auto generated classes by Zend Framework, where references are setup here
[Bindable] public var lecturers : Lecturers;
[Bindable] public var students : Students;
public function Globals()
{
// variables initialised
name = "";
email = "";
lecturersResult = new CallResponder();
studentsResult = new CallResponder();
lectAdd = new Button();
dropDownList_lecturerid = new DropDownList();
datagrid_lecturers = new DataGrid();
DatabaseTables = new Accordion();
lecturers = new Lecturers();
students = new Students();
}
// also useful when creating functions used between classes.
public function func1()
{
}
public function func2()
{
}
public function func3()
{
}
}
}
在主项目 mxml 文件中,我声明了 Globals 类的一个实例
public var g:Globals = new Globals();
使用新实例的mxml代码片段
<s:DataGrid id="datagrid_lecturers" x="10" y="10" width="860" height="300" editable="true" fontSize="12" requestedRowCount="4"
creationComplete="g.lecturersService.datagrid_lecturers_creationCompleteHandler(event,g,datagrid_lecturers,btnAddLecturer,btnDeleteLecturer,btnUpdateLecturer)"
selectionChange="g.lecturersService.datagrid_lecturers_selectionChangeHandler(event,g)">
另一个例子...
g.lecturersResult.token = g.lecturersService.getAllLecturers();
不利的一面是,我随身携带了很多行李,可能比必要的更具约束力。只要数据库很小,就可以正常工作。纯粹主义者不会喜欢这种方法。 Gloabls 通常被称为糟糕的编程实践......我同意,但对于那些刚开始的人......这不是一个糟糕的方式。随着您在这种环境中编码变得更好/更聪明,好的编程位可能会在以后一些。