为什么我的 History.newItem(someToken) 不触发 onValueChange()?

发布于 2024-12-21 12:22:42 字数 4297 浏览 6 评论 0原文

即使当我使用 History.fireCurrentHistoryState();

编辑 时它被正确触发:同一包中的所有类。代码更新 -

TestHistory.java

public class TestHistory implements EntryPoint, ValueChangeHandler<String> {

    static boolean isLoggedIn = false;
    static final String PAGENAME = "mainscreen";
    public void onModuleLoad()
    {
        History.addValueChangeHandler(this);

        String startToken = History.getToken();
        System.out.println("onModuleLoad Called..... start token= -------"+startToken+"--------");
        if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState(); //to execute onValueChange 1st time since 1st time history is not setup
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {

        String token = event.getValue();

        String args = "";
        int question = token.indexOf("?");
        if (question != -1) {
        args = token.substring(question + 1);
        token = token.substring(0, question);
        }

        if(!isLoggedIn)
        {
            if(token.isEmpty() || "login".equals(token))    //1st time opened the site normally
                new Login().display(false, RootPanel.get());
            else {
                new Login().display(true, RootPanel.get());
            }
        }
        else    //User has logged in
        {
            if(token.isEmpty() || "login".equals(token))
            {
                if(isLoggedIn)
                    Window.alert("Ur already logged in!!!");
                else
                    new Login().display(false, RootPanel.get());
            }
            else if("withdraw".equals(token))
                new Withdraw().display(RootPanel.get(), args);
            else if("deposit".equals(token))
                new Deposit().display(RootPanel.get(), args);
            else //token not clear
                Window.alert("Unrecognized token=" + token);
        }   

    }

}

Login.java

public class Login {
    static final String PAGENAME = "login";
    void display(final boolean hasTypedSomeToken, Panel myPanel) //Process login
    {
        System.out.println("login display called");
        Label displayLabel = new Label("This is the Login Page");
        Label enterName = new Label("Enter ur name");
        final TextBox txtName = new TextBox();
        Label enterPasswd = new Label("Enter ur Passwd");
        final TextBox txtPasswd = new TextBox();
        Button btnLogIn = new Button("Login", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                /* Real app will check DB. Here we r jst chckng d txt fields hv value */
                if(txtName.getValue().length()>0 && txtPasswd.getValue().length()>0)
                {
                    TestHistory.isLoggedIn = true;
                    if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }
                    else{
                        myPanel.clear();
                                            Label displayLabel = new Label("Thank U for logging.);
                                            myPanel.add(displayLabel);
                                         }

                }   
            }
        });         
        myPanel.clear();
        myPanel.add(displayLabel);
        myPanel.add(enterName);
        myPanel.add(txtName);
        myPanel.add(enterPasswd);
        myPanel.add(txtPasswd);
        myPanel.add(btnLogIn);
    }
}

Deposit.java

public class Deposit {
    static final String PAGENAME = "deposit";
    void display(Panel myPanel, String param)
    {
        System.out.println("deposit display called");
        myPanel.clear();
        Label displayLabel = new Label("This is the Deposit Page & ur parameter = "+param+")");
        myPanel.add(displayLabel);
    }   
}

Withdraw.java

//类似于deposit.java

Even though it is correctly fired when I use History.fireCurrentHistoryState();

EDIT: All classes in the same package. Code updated -

TestHistory.java

public class TestHistory implements EntryPoint, ValueChangeHandler<String> {

    static boolean isLoggedIn = false;
    static final String PAGENAME = "mainscreen";
    public void onModuleLoad()
    {
        History.addValueChangeHandler(this);

        String startToken = History.getToken();
        System.out.println("onModuleLoad Called..... start token= -------"+startToken+"--------");
        if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState(); //to execute onValueChange 1st time since 1st time history is not setup
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {

        String token = event.getValue();

        String args = "";
        int question = token.indexOf("?");
        if (question != -1) {
        args = token.substring(question + 1);
        token = token.substring(0, question);
        }

        if(!isLoggedIn)
        {
            if(token.isEmpty() || "login".equals(token))    //1st time opened the site normally
                new Login().display(false, RootPanel.get());
            else {
                new Login().display(true, RootPanel.get());
            }
        }
        else    //User has logged in
        {
            if(token.isEmpty() || "login".equals(token))
            {
                if(isLoggedIn)
                    Window.alert("Ur already logged in!!!");
                else
                    new Login().display(false, RootPanel.get());
            }
            else if("withdraw".equals(token))
                new Withdraw().display(RootPanel.get(), args);
            else if("deposit".equals(token))
                new Deposit().display(RootPanel.get(), args);
            else //token not clear
                Window.alert("Unrecognized token=" + token);
        }   

    }

}

Login.java

public class Login {
    static final String PAGENAME = "login";
    void display(final boolean hasTypedSomeToken, Panel myPanel) //Process login
    {
        System.out.println("login display called");
        Label displayLabel = new Label("This is the Login Page");
        Label enterName = new Label("Enter ur name");
        final TextBox txtName = new TextBox();
        Label enterPasswd = new Label("Enter ur Passwd");
        final TextBox txtPasswd = new TextBox();
        Button btnLogIn = new Button("Login", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                /* Real app will check DB. Here we r jst chckng d txt fields hv value */
                if(txtName.getValue().length()>0 && txtPasswd.getValue().length()>0)
                {
                    TestHistory.isLoggedIn = true;
                    if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }
                    else{
                        myPanel.clear();
                                            Label displayLabel = new Label("Thank U for logging.);
                                            myPanel.add(displayLabel);
                                         }

                }   
            }
        });         
        myPanel.clear();
        myPanel.add(displayLabel);
        myPanel.add(enterName);
        myPanel.add(txtName);
        myPanel.add(enterPasswd);
        myPanel.add(txtPasswd);
        myPanel.add(btnLogIn);
    }
}

Deposit.java

public class Deposit {
    static final String PAGENAME = "deposit";
    void display(Panel myPanel, String param)
    {
        System.out.println("deposit display called");
        myPanel.clear();
        Label displayLabel = new Label("This is the Deposit Page & ur parameter = "+param+")");
        myPanel.add(displayLabel);
    }   
}

Withdraw.java

//similar to deposit.java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-12-28 12:22:42

问题在于 History.newItem() 的使用。当我使用添加书签的 url 并使用新令牌调用 History.newItem() 时,出现了问题。由于同一内部页面已经存在一个令牌,并且我给了它一个新令牌,因此存在一些混乱,并且 onValueChange() 没有被调用。

现在我很清楚,当当前视图没有附加令牌时,应该使用 History.newItem() 来用令牌标记视图。一般来说,当用户正常打开站点(没有令牌)时,我们应该使用history.newItem来标记第一个视图。

另外值得注意的是 History.fireCurrentHistoryState() 仅使用当前令牌调用 onValueChange。通过查看 GWT 的代码,我发现 History.newItem() 只是调用 History.fireCurrentHistoryState()

实际上,如果我

if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState();

在代码中替换为

   if(startToken.isEmpty())
            History.newItem("login");
        else
            History.fireCurrentHistoryState();

& 代码也

if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }

它的

if(hasTypedSomeToken) {
                    History.fireCurrentHistoryState();
                    System.out.println("getToken() in Login =   "+History.getToken());
                }

运行得很好。

The problem was with the usage of History.newItem(). the problem was occuring when I was using the bookmarked url and calling History.newItem() with a new token. Since already a token was present for the same internal page and I was giving it a new token so there was some confusion and onValueChange() was not being called.

Now Im clear that History.newItem() should be used when there is no token attached to the current view to mark the view with a token. Generally when a user opens a site normally (with no token), we should use history.newItem to mark the 1st view.

Also worth noting is that History.fireCurrentHistoryState() just calls onValueChange with the current token. And by going through the GWT's Code I found that History.newItem() simply calls History.fireCurrentHistoryState()

Actually if I replace

if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState();

in my code with

   if(startToken.isEmpty())
            History.newItem("login");
        else
            History.fireCurrentHistoryState();

& also the code

if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }

with

if(hasTypedSomeToken) {
                    History.fireCurrentHistoryState();
                    System.out.println("getToken() in Login =   "+History.getToken());
                }

it works pretty well.

紅太極 2024-12-28 12:22:42

尽管 newItem(...) 通常会触发一个事件,但如果当前令牌与您尝试添加的令牌相同,则该事件是无操作的。如果情况并非如此,则说明您的实现存在问题。

Although newItem(...) generally fires an event, it is a no-op if the current token is the same as the one you're trying to add. If that's not the case, there's a problem with your implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文