Viewstack访问

时间:2014-07-23 16:32:14

标签: flash flex flash-builder

你好在Brian的帮助下我有这个alomost工作,但我错过了一些不让它100%工作的东西。

当我追踪这个时,即使我使用任何用户登录,我也会在命令面板上获得-1。 我希望如果用户登录取决于他们的userRole,则视图堆栈会转到他们的视图。

下面你会看到我的代码。 任何人都可以帮忙:(

<mx:ViewStack id="parentStack" width="1280" height="768">
    <s:NavigatorContent id="loginVS" width="100%" height="100%" label="Login">
        <forms:MainSiteForm id="mainForm" horizontalCenter="0" verticalCenter="0"/>
    </s:NavigatorContent>
    <s:NavigatorContent id="adminVS" width="100%" height="100%" label="Admin">
        <s:ModuleLoader url="views/Administrator.swf"/>
    </s:NavigatorContent>
    <s:NavigatorContent id="salesVS" width="100%" height="100%" label="Sales">
    </s:NavigatorContent>
    <s:NavigatorContent id="designersVS" width="100%" height="100%" label="Designers">
    </s:NavigatorContent>
    <s:NavigatorContent id="retailersVS" width="100%" height="100%" label="Retailers">
    </s:NavigatorContent>
</mx:ViewStack>

<fx:Script>
        <![CDATA[
            import mx.containers.ViewStack;
            import mx.controls.Alert;
            //import mx.core.Container;
            import mx.core.FlexGlobals;
            import mx.validators.Validator;

            [Bindable]
            private var validatorArr:Array;
            private var loader:URLLoader;
            private var externalXML:XML;

            /**
             * Initialization method
             */
            public function init():void {
                // Add validators to array for validating all at one time
                validatorArr = new Array();
                validatorArr.push(useridValidator);
                validatorArr.push(passwordValidator);
            }

            /**
             * User's information checking
             * based on the inputted data
             */
            public function loginCheck():void  {
                // Starting to validate
                var errorArr:Array = Validator.validateAll(validatorArr)
                // if the error array contain any data, =&gt; error
                var isValid:Boolean = errorArr.length == 0;

                if (isValid) {
                    // Load user data
                    doLogin();
                }
            }

            private function doLogin():void {
                // Point to the data file's path
                var request:URLRequest = new URLRequest("data/Users.xml");

                loader = new URLLoader();
                try {
                    loader.load(request);
                } catch (error:SecurityError) {
                    trace("A SecurityError has occurred.");
                }

                // If error, go to errorHandler function
                loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
                // once the load action was complated, go to loaderCompleteHandler() for
                // checking user information
                loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
            }

            private function loaderCompleteHandler(event:Event):void {
                var logged:Boolean = false;
                try {
                    // Convert data into E4X class for easy access to elements
                    externalXML = new XML(loader.data);

                    // Loop through the user list to check
                    // if the inputted user id and password are valid
                    for each (var user:XML in externalXML.elements()) {
                        if (user.userID == userID.text && user.userPW == userPW.text) {
                            // user has an valid account
                            logged = true;
                            // Don't need to continue
                            break;
                        }
                    }
                    if (logged) {
                        // Redirect user function
                        var indexArr:Array = new Array("Login", "Admin", "Sales", "Designers", "Retailers");
                        var stackIndex:int = indexArr.indexOf(user.userRole);
                        FlexGlobals.topLevelApplication.parentStack.selectedIndex = stackIndex;
                        trace(stackIndex);
                        //Alert.show('Congratulation, you logged in!', 'Information');
                    } else {
                        Alert.show('User ID or Password is not correct, please try again!', 'Error');
                    }

                } catch (e:TypeError) {
                    trace("Could not parse the XML file.");
                }
            }
            private function errorHandler(e:IOErrorEvent):void {
                Alert.show("Had problem loading the XML File.");
            }

        ]]>
    </fx:Script>

----------------------------------------------- --- XML文件

<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user>
        <userID>admin</userID>
        <userPW>password</userPW>
        <userRole>Admin</userRole>
    </user>
    <user>
        <userID>sales</userID>
        <userPW>password</userPW>
        <userRole>Sales</userRole>
    </user>
    <user>
        <userID>designer</userID>
        <userPW>password</userPW>
        <userRole>Designers</userRole>
    </user>
    <user>
        <userID>buyer</userID>
        <userPW>password</userPW>
        <userRole>Retailers</userRole>
    </user>
</users>

1 个答案:

答案 0 :(得分:0)

一种解决方案是使用selectedIndex并在数组中查找所需的索引。重要的是确保您的数组条目与您从user.userRole获得的角色名称匹配:

if (logged) {
    // Redirect user function
    var indexArr:Array = new Array("Login", "Admin", "Sales", "Designers", "Retailers");
    //Make sure that `user.userRole` is a String, not e.g. an XML object's value.
    var stackIndex:int = indexArr.indexOf(user.userRole);
    FlexGlobals.topLevelApplication.parentStack.selectedIndex = stackIndex;

请注意,将上述逻辑拉入Main.mxml类中的函数将是一个很好的改进;这使得ViewStack的引用不会渗透到代码的其他部分。