PDA

View Full Version : global object - make component more dynamic


ljonny18
12-06-2006, 01:14 PM
Hi

I have an application that has different users types defined by a global variable.

I have an interface created in Flex 2 - the interface contains a dataGrid with buttons (functionalities) below….

The buttons displayed are different for each user type – e.g.:

Admin Users can view, add, delete and modify – so they can see an:
- view button
- add button
- delete button
- modify button

Whereas, a general user can only view and add – so they can only see:
- view button
- add button

etc………..

currently I have this view as 2 separate views (mxml components) where if the user type is an “Adim User” then they are directed to the: adim_homepage.mxml and if the user type is a “Gernaeral User” then they are directed to the general_homepage.mxml etc………..

what I want to do (as more user type could emerge in the near future) is to have one homepage component (homPage.mxml) and then contain the buttons within something like a global object – so if the user is an “Admin User” then the object containing the admin buttons is displayed and so on etc….. making the application more dynamic….


Is what I want to do possible within Flex 2 ??? and is my thinking about the way to go about doing it along the correct lines ??? or is there a better method available???

Could someone offer me some advice on this one?

Thanks
Jon.

CDHBookingEdge
12-06-2006, 02:55 PM
Hmm interesting one Jonny and I think some stuff I ran up on yesterday might work for you. Now I'm not 100% sure if from what I read of the design of your program whether the buttons are actually ON the datagrid or not. But really the concept should work either way. Why don't you use a PopupMenuButton? Then you can have some initialization code to set up the options based on the user. So here's what I'm thinking in a kind of Pseudo code and I'll do it where the buttons are not on the grid


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" frameRate="120" width="100%" height="100%" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.*;
import mx.controls.*;

// constants to define the action chosen for the button
private const ACTION_VIEW:int = 0;
private const ACTION_ADD:int = 1;
private const ACTION_DEL:int = 2;
private const ACTION_MOD:int = 3;

// constants for the user type
private const USR_ADMIN:int = 0;
private const USR_GENERAL:int = 1;

// varibables
private var nActionType:int = ACTION_VIEW;
private var nUserType:int = USR_ADMIN;

private function doAction() : void
{
switch (nActionType)
{
case ACTION_VIEW:
DoViewAction();
break;
case ACTION_ADD:
DoAddAction();
break;
case ACTION_DEL:
DoDeleteAction();
break;
case ACTION_MOD:
DoModifyAction();
break;
}
}

private function DoViewAction() : void
{
Alert.show("I'm gonna View");
}

private function DoAddAction() : void
{
Alert.show("I'm gonna Add");
}

private function DoDeleteAction() : void
{
Alert.show("I'm gonna Delete");
}

private function DoModifyAction() : void
{
Alert.show("I'm gonna modify");
}

private function initActionButtonMenu():void
{
var adminItems:Object = [{label: "View"}, {label: "Add"}, {label: "Delete"}, {label: "Modify"}];
var generalItems:Object = [{label: "View"}, {label: "Add"}];

if (nUserType == USR_ADMIN)
actionButton.dataProvider = adminItems;
else
actionButton.dataProvider = generalItems;
actionButton.addEventListener(MenuEvent.ITEM_CLICK , itemClickHandler);
}

// Define the event listener for the Menu control's itemClick event.
private function itemClickHandler(event:MenuEvent):void
{
var label:String = event.item.label;

actionButton.label = label;
actionButton.close();
nActionType = event.index;
}
]]>
</mx:Script>
<mx:Panel id="myDataPanel">
<mx:VBox>
<mx:DataGrid/>
<mx:PopUpMenuButton id="actionButton" label="View" creationComplete="initActionButtonMenu();" click="doAction();" width="135" />
</mx:VBox>
</mx:Panel>
</mx:Application>


Now if you look at nUserType you'll see it's set to Admin (USR_ADMIN) and therefore on this run it would show all the items. if you change it to USR_GENERAL then it will only show View and Add. It also takes up a lot less real estate on the screen to handle it and that will be especially important if you have the button(s) being hosted in the grid that cuts your columns down and all that. I'll leave you with that and see what further questions you might have on it rather than trying to guess some you might have. ;-)

Let me know,
Christopher