0

只有当它是管理员时,我才想向用户显示一些按钮。我想过使用状态,但是我不知道如何访问其他 mxml 页面中的按钮来定义它们的可见性。

4

1 回答 1

3

Basically, at some point you're application will need to store the information that lets you determine whether the user is an admin or not.

Without knowing anything about your setup; the easiest way is going to be store that value in a static variable somewhere; something like this:

public static var isUserAdmin : Boolean = true;

Now you can access that property anywhere in the app by referencing the class name. Static variables exist on a class; not on an instance of a class.

You can use this to control states inside components if that is what you want. Somewhere in the component, perhaps in an initialize event handler, you can do this:

if(myClassWithStaticVaraibles.isUserAdmin){
  currentState = 'adminState';
} else {
  currentState = 'nonAdminState';
}

You can also use this to toggle the visibility of buttons or other UI Elements. This will show a button if the user is an admin:

<s:Button visible="{myClassWithStaticVariables.isUserAdmin}" />

This will hide a button for user admins:

<s:Button visible="{!myClassWithStaticVariables.isUserAdmin}" />

There are more complex approaches than using static variables; such as using a framework, such as Swiz or Robotlegs, that support dependency injection of a Singleton like class. In "real world" applications; use of such frameworks seems to be much more common than the static variable approach. But, the approach is the same:

  1. Store value somewhere
  2. Access value in view
  3. Modify views display based on the value
于 2013-07-24T00:46:10.790 回答