Flash Viewstack访问
你好,在布赖恩的帮助下,我有一个很好的工作,但我错过了一些不能让它100%工作的东西 当我跟踪这一点时,即使我使用任何用户登录,我也会在命令面板上得到-1。 我希望,如果用户登录,根据他们的用户角色,viewstack进入他们的视图 下面你会看到我的代码。 还有人可以帮忙吗Flash Viewstack访问,flash,apache-flex,flash-builder,Flash,Apache Flex,Flash Builder,你好,在布赖恩的帮助下,我有一个很好的工作,但我错过了一些不能让它100%工作的东西 当我跟踪这一点时,即使我使用任何用户登录,我也会在命令面板上得到-1。 我希望,如果用户登录,根据他们的用户角色,viewstack进入他们的视图 下面你会看到我的代码。 还有人可以帮忙吗 <mx:ViewStack id="parentStack" width="1280" height="768"> <s:NavigatorContent id="loginVS" width="1
<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, => 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>
一种解决方案是使用selectedIndex并在数组中查找所需的索引。重要的一点是确保数组项与从user.userRole获得的角色名匹配
请注意,将上述逻辑拉入Main.mxml类中的函数将是一个很好的改进;这可以防止对ViewStack的引用泄漏到代码的其他部分。你好,Brian…哇,这很好…我明天第一件事就是尝试一下,让你知道…我如何改进你说的方式??我该怎么做才能做到这一点:你好,Brain,我刚刚测试了代码,但它什么也没做:它停留在登录视图上还是什么都没有我的朋友:在stackIndex行上,你得到的值是-1吗?如果没有-如果您获得的是有效索引-请确保您在数组中保持登录-确保数组索引与ViewStack索引匹配非常重要。
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;