在骨干网上验证后阻止页面重新加载

时间:2013-10-17 11:43:42

标签: javascript html backbone.js

我有这个观点:

 return Backbone.Marionette.ItemView.extend({ 
    template: template,

    ui: {
      form: '#login',
      button: '#submitbutton'
    },

    onRender: function() {
      this.ui.form.on('submit', function(e) {

        // e.preventDefault();

        mylogin = new login();

        mylogin.save({boxid:$("#boxid").val(),password:$("#passwordid").val(),validate:true});

             vent.trigger('navigate', 'home');

        // if(myLogin.validationError) {
        //     vent.trigger('navigate', 'home');
        // }

        return false;
      });
    }
  });

和这个模型:

return Backbone.Model.extend({



  validate: function(attrs, options){
    if(attrs.boxid.length < 10)
      {
        return "user id must be more than 10 characters";
        // return "BoxID should be greater than 10";
      }
   else if(attrs.password.length < 10)
      { 
         return "password must be more than 10 characters";
        // return "Password should be greater than 10";
      }
   else if((attrs.boxid!=myBoxid)||(attrs.password!=myPassword))
      {
          return "Your login credentials are incorrect";  
      }

  },





   });

这个登录html模板:

<div class>
    <div>Information</div>
    <div>
        <form id="login" name="login" method="post">  
            <div>
                <label for="boxid">BoxID</label>  
                <input id="boxid" name="box" placeholder="Enter your box ID">
            </div>
            <div>
                <label for="password">Password</label>  
                <input id="passwordid" type="password" name="password" placeholder="Password">
            </div>
            <label id="errormsg"></label>
            <button id="submitbutton">Login</button>
        </form>  
    </div>
</div>

问题是,每次用户输入错误的凭据时页面都会重新加载,有什么方法可以阻止这种情况吗?

1 个答案:

答案 0 :(得分:0)

为了完成这项工作,你应该支持e.preventDefault();行。

更重要的是,该模型没有url函数http://backbonejs.org/#Model-url

return Backbone.Model.extend({
    url: function () {
        // see the docs, it can be a url string or a function
    }
    validate: function(attrs, options){
    },
});