一起使用数据绑定提交、sammy.js 和knockout.js 的奇怪重定向 [英] Weird redirect using data-bind submit, sammy.js and knockout.js together

查看:16
本文介绍了一起使用数据绑定提交、sammy.js 和knockout.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.

推荐答案

经过多次搜索,在 SO 上没有找到任何答案,我最终找到了这个:

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

并将我的代码修改为:

Sammy(function() {

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

}).run();

这样当表单在我的页面上提交时,Sammy 不会尝试做任何特别的事情.由于我使用的是 Knockout,因此我不打算将 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 和knockout.js 的奇怪重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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