骨干验证不工作 [英] Backbone validate not working

查看:83
本文介绍了骨干验证不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是起步backboneJs。

Im just starting out backboneJs.

我没有使用VisualStudio2012一个简单的例子。
我加了骨干力量。

I did a simple example using VisualStudio2012. I added backbone.

在我的main.js

in my main.js

var Person = Backbone.Model.extend({
defaults: {
    name: 'Mr Steel',
    age: 26,
    occupation: 'FrontEnd Web Developer'
},

validate: function(attrs) {
    if (attrs.age < 0) {
        return 'Age must be a positive number.';
    }
},

work: function() {
    return this.get('name') + ' is working.';
}
});

但是,当我去到浏览器>控制台。并键入以下..验证甚而不被触发。

But when I go to browser > console. And type the following in .. validate doesnt even get triggered.

var person = new Person();
person.get('age');//26
person.set('age', -25);//sets -25

和它设置的年龄-25。这很奇怪!!请帮助。

And it sets the age to -25. thats weird!! please help.

推荐答案

您必须通过 {验证:真} 参数为设置方法例如

You have to pass {validate:true} param to set method e.g.

person.set('年龄',-25,{验证:真});

这篇关于骨干验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆