Wicket :通知页面模型是否已更改

发布于 2024-09-11 13:48:12 字数 207 浏览 2 评论 0 原文

问题是这样的;网页包含多个表单元素,用户可以通过保存按钮更改和保存这些元素,也可以放弃更改。

如果用户尝试在不保存更改的情况下离开页面,我需要一个模式窗口来弹出,询问用户是否想在离开页面之前保存更改。

我将如何检查页面/表单模型自首次加载以来是否已被用户更改,以及当单击任何页面链接时如何启动此检查?

任何回复或建议将不胜感激,

谢谢。

The problem is like this; A webpage contains multiple form elements, which can be altered and saved by the user via a save button, or changes can be discarded.

If the user attempts to navigate away from the page without saving the changes, I need a modal window to pop us asking if the user would like to save the changes before leaving the page.

How would I go about checking to see if the page/form model had been changed by a user since it was first loaded, and how could I initiate this check when any page link is clicked?

Any response or suggestions would be much appreciated,

Thanks.

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

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

发布评论

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

评论(1

狂之美人 2024-09-18 13:48:12

我认为您会寻找一种仅使用 javascript 的解决方案,通常打包为 wicket 行为。

实现取决于您使用的 javascript 库,这里是一些原型代码:

var windowdirty = false;
var windowdirtyinitialized = false;

function initwindowdirty(){
    if(windowdirtyinitialized)return;
    windowdirtyinitialized=true;

    Event.observe(window,"beforeunload",function(){
        return (!windowdirty || 
          confirm("You have started entering values, do you really want to leave");
    });
}

function monitor(componentId){
    $(componentId).observe("change",function(){
        windowdirty = true;
    });
}

function undirty(){
    windowdirty=false;
}

我们将其放入名为 DontLeaveBehavior.js 的文件中 这是

使用此 javascript 文件的行为:

public class DontLeaveBehavior extends AbstractBehavior{

    /**
     * {@inheritDoc}
     */
    @Override
    public void renderHead(final IHeaderResponse response){
        response.renderJavascriptReference(new JavascriptResourceReference(DontLeaveBehavior.class,
            "DontLeaveBehavior.js"));
        response.renderOnDomReadyJavascript("initwindowdirty();");
        super.renderHead(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void bind(final Component component){
        super.bind(component);
        component.setOutputMarkupId(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onRendered(final Component component){
        final Response response = RequestCycle.get().getResponse();
        response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
        response.write("monitor('" + component.getMarkupId() + "');");
        response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
    }

}

现在这是一个自动将此行为分配给其所有子级的页面它们是文本组件:

public class Mypage extends WebPage{

    ...

    private boolean behaviorAssigned = false;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onBeforeRender(){
        if(!behaviorAssigned){
            behaviorAssigned=true;
            visitChildren(new IVisitor<Component>(){

                @Override
                public Object component(Component component){
                    if(component instanceof AbstractTextComponent<?>){
                        component.add(new DontLeaveBehavior());
                    }
                    return null;
                }
            });
        }
        super.onBeforeRender();
    }

}

最后但并非最不重要的一点是,您的提交按钮当然必须调用 undirty() 。

这些都没有经过测试,因为我现在必须回家与我的妻子和家人共进晚餐。孩子们(当然,这比 wicket 编码更有趣),但它应该可以帮助您入门。

原型特定部分应该可以轻松移植到任何其他 javascript 库,但如果您不知道自己在做什么,则可能不应该在没有库的情况下执行此操作。


编辑:

我创建了一个新版本,可与原型和 mootools 以及 将其发布到我的博客上。此版本仅安装到表单组件,并通过 javascript 自动将其自身附加到子组件。

再次编辑:那里,现在链接正在工作

I think you would be looking for a javascript-only solution, usually packaged as a wicket behavior.

implementation depends on the javascript library you use, here is some prototype code:

var windowdirty = false;
var windowdirtyinitialized = false;

function initwindowdirty(){
    if(windowdirtyinitialized)return;
    windowdirtyinitialized=true;

    Event.observe(window,"beforeunload",function(){
        return (!windowdirty || 
          confirm("You have started entering values, do you really want to leave");
    });
}

function monitor(componentId){
    $(componentId).observe("change",function(){
        windowdirty = true;
    });
}

function undirty(){
    windowdirty=false;
}

We'll put this in a File called DontLeaveBehavior.js

Here's a behavior that uses this javascript file:

public class DontLeaveBehavior extends AbstractBehavior{

    /**
     * {@inheritDoc}
     */
    @Override
    public void renderHead(final IHeaderResponse response){
        response.renderJavascriptReference(new JavascriptResourceReference(DontLeaveBehavior.class,
            "DontLeaveBehavior.js"));
        response.renderOnDomReadyJavascript("initwindowdirty();");
        super.renderHead(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void bind(final Component component){
        super.bind(component);
        component.setOutputMarkupId(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onRendered(final Component component){
        final Response response = RequestCycle.get().getResponse();
        response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
        response.write("monitor('" + component.getMarkupId() + "');");
        response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
    }

}

Now here's a page that automatically assigns this behavior to all of it's children that are text components:

public class Mypage extends WebPage{

    ...

    private boolean behaviorAssigned = false;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onBeforeRender(){
        if(!behaviorAssigned){
            behaviorAssigned=true;
            visitChildren(new IVisitor<Component>(){

                @Override
                public Object component(Component component){
                    if(component instanceof AbstractTextComponent<?>){
                        component.add(new DontLeaveBehavior());
                    }
                    return null;
                }
            });
        }
        super.onBeforeRender();
    }

}

and last but not least, your submit button has to call undirty() of course.

None of this has been tested, because I have to go home now to have dinner with my wife & kids (which is even more fun than wicket coding, of course), but it should get you started.

The prototype specific portion should be easily ported to any other javascript lib, but you probably shouldn't do it without a lib if you don't know what you're doing.


Edit:

I have created a new version of this that works with prototype and mootools and posted it on my weblog. This version is only installed to the form component and automatically attaches itself to the children via javascript.

EDIT again: there, now the link is working

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