18

I have a form like this on my page:

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

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();

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

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.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • For me, at least, the field it added was ko_unique_1=true, a fact I bring only because I Googled it and nothing came up. If this page said it I would have solved this problem days ago! – C. Warren Dale Nov 13 '14 at 20:24

1 Answers1

49

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();

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.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • Yup. Just killed quite a few hours wondering why my form submit was working as expected w/ knockout, but the results completely erased when a mysterious new path (my domain's root) is applied as location and the sammy route bound to '' takes over. Yikes. – Steven Francolla Aug 16 '13 at 16:46
  • 1
    same here, would have taken hours to track this down, thx for posting the solution. the clean way of doing this is probably wrapping this into a sammy plugin as suggested in one of the last google group posts. – philipphoffmann Apr 09 '14 at 21:06