使用数据绑定提交,sammy.js和基因敲除.js一起进行怪异的重定向 [英] Weird redirect using data-bind submit, sammy.js and knockout.js together

查看:78
本文介绍了使用数据绑定提交,sammy.js和基因敲除.js一起进行怪异的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有这样的表格:

I have a form like this on my page:

<form data-bind="submit: AddFolder"></form>

如果我的JS脚本中有此代码(当然,我已经删除了所有不相关的代码,并进行了测试以确保我仍然可以仅使用此代码来重新创建代码,通常,您在其中会包含更多代码,如.get)和.post函数):

If I have this code in my JS script (of course I've removed all of the unrelated code and tested to make sure I can still recreate with only this code, normally you'll have more code inside here like .get and .post functions):

Sammy(function() {
}).run();

提交表单后,页面将重定向到一个奇怪的URL,例如?ko_unique=1

When the form is submitted, the page redirects to a weird URL like ?ko_unique=1

如果我从脚本中删除了Sammy部分,则不会发生.我认为这与事件冒泡有关,Sammy和Knockout都钩住了onSubmit,而浏览器仅尊重最后一个调用函数的返回值.

If I remove the Sammy part from my script, this doesn't happen. I think it has something to do with event bubbling and Sammy and Knockout both hooking the onSubmit, and the browser only respecting the return value from the last function called.

推荐答案

经过大量搜索,但在此处未找到任何答案,我最终发现了这一点:

After much searching, and not finding any answers here on SO, I ended up finding this:

https://groups.google.com/forum/?fromgroups#!topic/sammyjs/EYW-2Ygk3z8

并将我的代码修改为此:

And modified my code to this:

Sammy(function() {

    // Override this function so that Sammy doesn't mess with forms
    this._checkFormSubmission = function(form) {
        return (false);
    };

}).run();

因此,在我的页面上提交表单时,Sammy绝不会尝试做任何特殊的事情.由于我使用的是淘汰赛,因此我不打算将Sammy用于任何形式.如果您想要更复杂的代码或插件版本,则可以看到上面的URL,但对我来说,我怀疑大多数使用KO的人都可以使用更少的代码,并且更容易禁用此Sammy功能.

So that Sammy never attempts to do anything special when a form is submitted on my page. Since I'm using Knockout, I don't plan on using Sammy for any forms. If you want more complex code or a plugin version you can see the above URL, but for me, and I suspect for most using KO, it's less code and easier to just disable this Sammy feature.

这篇关于使用数据绑定提交,sammy.js和基因敲除.js一起进行怪异的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆