简体   繁体   English

返回BackboneJS上的model.save值

[英]return value of a model.save on BackboneJS

I have function on the Backbone view: 我在Backbone视图上有功能:

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

        mylogin = new login();

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

        if(valid != false)
        {
               vent.trigger('navigate', 'home');
        }

        return false;
      });

and this function on the model: 和模型上的这个功能:

validate: function(attrs, options){
    if(attrs.boxid.length < 10)
      {
        return false;
      }
   else if(attrs.password.length < 10)
      { 
        return false;
      }
   else
      {
        if((attrs.boxid!=myBoxid)||(attrs.password!=myPassword))
        {
          return false;
        }
        else
        {
        return true;
        }
      }

I want to trigger this vent.trigger('navigate', 'home'); 我想触发这个vent.trigger('navigate', 'home'); when the validation is correct, but it doesn't work, any idea? 当验证是正确的,但它不起作用,任何想法? what I am doing wrong? 我做错了什么?

I think you have a very common mistake in your assumption about how backbone validate works. 我认为你在骨干validate如何工作的假设中有一个非常常见的错误。 If your validation function returns anything, that is an indication of an error. 如果验证函数返回任何内容,则表示存在错误。 So...for one thing, you're backward on that. 所以...首先,你是落后的。 You're returning false when validation fails, and you should be returning a message as to why it failed. 当验证失败时,您将返回false,并且您应该返回一条消息,说明它失败的原因。 Also when you do validate:true , that's not an option you're passing there, you're actually setting an attribute on your model named validate. 此外,当您validate:true ,这不是您传递的选项,您实际上是在名为validate的模型上设置属性。 Options are passed as a second object. 选项作为第二个对象传递。

Read these docs carefully: http://backbonejs.org/#Model-validate 仔细阅读这些文档: http//backbonejs.org/#Model-validate

Try doing something like this. 尝试做这样的事情。 Sorry without more information this is the best I can do to help. 抱歉没有更多信息,这是我能帮到的最好的。 Cheers. 干杯。

View: 视图:

onRender: function() {
  this.ui.form.on('submit', function() {
    mylogin = new login();

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

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

    return false;
  });
}

Model: 模型:

validate: function(attrs, options){
  if(attrs.boxid.length < 10) {
    return "user id must be more than 10 characters";
  } 

  if(attrs.password.length < 10) {
    return "password must be more than 10 characters";
  } 

  if((attrs.boxid!=myBoxid)||(attrs.password!=myPassword)) {
    return "Your login credentials are incorrect";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM