Backbone 1.0.0 版 API _ Backbone.Model validate 解析

  最近一段时间在学习backbone,发现网上的很多资料都是旧版本的,有一些儿不适用于backbone1.0.0的最新版,尤其validate这个方法的用法。

        最老的旧版中,说的是:调用model的set方法会默认调用model的validate方法;稍微旧的版本中,说到调用model的set方法,传入{silent: true},可以启动validate校验,调用save方法会强制进行校验;在1.0.0的版本中,我们来看下官网的explain:

validatemodel.validate(attributes, options) 
This method is left undefined, and you're encouraged to override it with your custom validation logic, if you have any that can be performed in JavaScript. By defaultvalidate is called before save, but can also be called before set if {validate:true}is passed. The validate method is passed the model attributes, as well as the options from set or save. If the attributes are valid, don't return anything from validate; if they are invalid, return an error of your choosing. It can be as simple as a string error message to be displayed, or a complete error object that describes the error programmatically. If validate returns an error, save will not continue, and the model attributes will not be modified on the server. Failed validations trigger an "invalid"event, and set the validationError property on the model with the value returned by this method.

 

        validate方法在Backbone.Model中留下来,在继承Model的时候重写(自定义)validate方法。调用save方法前默认会调用validate方法,当然调用set方法,如果传入{validate: true}也可以调用validate方法。

        如果向model添加的attribute通过校验,validate不会返回任何值,如果没有通过校验(invalid),会返回你在validate方法中定义的错误。校验失败,会触发invalid事件,并且填充model的validateErro属性值。

 

Js代码 
  1. var Chapter = Backbone.Model.extend({  
  2.   validate: function(attrs, options) {  
  3.     if (attrs.end < attrs.start) {  
  4.       return "can't end before it starts";  
  5.     }  
  6.   }  
  7. });  
  8.   
  9. var one = new Chapter({  
  10.   title : "Chapter One: The Beginning"  
  11. });  
  12.   
  13. one.on("invalid"function(model, error) {  
  14.   alert(model.get("title") + " " + error);  
  15. });  
  16.   
  17. one.save({  
  18.   start: 15,  
  19.   end:   10  
  20. });  

 

如果你在chrome下跑个demo,你会发现save方法如果不传入{validate: true},即使验证不通过,one.toJSON()会发现start和end的值还是改变了

 

Js代码 
  1. var Chapter = Backbone.Model.extend({  
  2.   validate: function(attrs, options) {  
  3.     if (attrs.end < attrs.start) {  
  4.       return "can't end before it starts";  
  5.     }  
  6.   }  
  7. });  
  8.   
  9. var one = new Chapter({  
  10.   title : "Chapter One: The Beginning"  
  11. });  
  12.   
  13. one.on("invalid"function(model, error) {  
  14.   alert(model.get("title") + " " + error);  
  15. });  
  16.   
  17. one.save({  
  18.   start: 15,  
  19.   end:   10  
  20. });  
  21. 输出:false  
  22. one.toJSON()  
Js代码 
  1. 输出:Object {title: "Chapter One: The Beginning", start: 15, end: 10}  
  2. one.save({  
  3.   start: 25,  
  4.   end:   10  
  5. });  
  6. 输出:false  
  7. one.toJSON()  
  8. 输出:Object {title: "Chapter One: The Beginning", start: 25, end: 10}  
  9. one.save({  
  10.   start: 35,  
  11.   end:   10  
  12. }, {validate: true});  
  13. 输出:false  
  14. one.toJSON()  
  15. 输出:Object {title: "Chapter One: The Beginning", start: 25, end: 10}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值