开发者

Why does sammy.js never use post route?

开发者 https://www.devze.com 2023-01-19 15:00 出处:网络
OK so I just started using sammy.js , I don\'t it very well but I was able to get the basics working pretty quickly.

OK so I just started using sammy.js , I don't it very well but I was able to get the basics working pretty quickly. HTML form I'm using

<form id="contact_form" action="#/form" method="post">
<label for="name">Name:</label><input  class="textbox" type="text" name="name" id="name" />
<label for="email">E-mail:</label><input  class="textbox" type="text" name="email" id="email" />
<label for="message">Message:</label><textarea class="textbox" name="message"  id="message"></textarea>
<button type="submit" name="submit" value="Submit" id="submit" />
<p id="message_outcome"></p>

js I'm Using to route my urls

var app = $.sammy(function(){
    this.element_selector = '#page1';
    this.get('#/about',function(context){
        $("ul#nav li").removeClass("active"); //removes "active" class
        $('#nav_1').addClass("active"); //adds "active" class to the tab that is selected
        $.post('home/about','', function(resp){
            $('#page1').html(resp);
            $('#page_container').cycle(0); //the number of the div that will be shown on click
        });
    });

    this.post('#/form', function(context){
       this.log('Form Submission');

    });
});


$(function(){
    app.run('#/about');
});

When the url has #/form in it, I get a console log that says its running route get('#/form') .. any ideas?

Here is some more information that may help

Jquery Plugin I'm using aside from sammy

http://jquery.malsup.com/cycle/

This is what my chrome console says when I click submit on my form. [Sun Oct 10 2010 05:29:27 GMT-0400 (Eastern Daylight Time)] runRou开发者_Python百科te get #/form /public/js/lib/sammy.js:96 [Sun Oct 10 2010 05:29:27 GMT-0400 (Eastern Daylight Time)] 404 Not Found get #/form Error: 404 Not Found get #/form


Another thing to keep in mind for post routes is to understand how sammy works. When Sammy is initialized it gets bounded to a jQuery selector.

var app = $.sammy('#content', function() {
   ...
});

For post routes, Sammy binds to all the forms inside the selector used for initializing (#content in our example) the application and listens for the click events generated by form submit as these gets bubbled up to the parent. Now, when it sees the submit event for form it will execute the corresponding route if the form points to a route.

On thing to keep in mind is that if your form is outside the main selector (e.g. in header which is outside #content) then sammy will never receive the submit event and post route will never execute.


That's because you are running an HTTP GET to '#/form' if you just typed a URL into your browser. The way to trigger the POST would be to submit you form. You want to set up both get and post routes. Get shows the form and post handles the submission.


I've recently discovered that Sammy.js catching exception coming from some erroneous JavaScript code inside app definition exchanges the original request to some generic GET with the originally sent URL. This is really confusing and all you have to do is debug your script for some function/method misuse. When you get all cleared out, the original POST gets through like magic.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号