在设置或存储属性的时候进行数据校验

 1 Person = Backbone.Model.extend({

 2     //如果从validate中返回字符串了,Backbone就会抛个实例异常

 3     validate: function(attributes) {

 4         if (attributes.age < 0 && attributes.name != "Dr Manhatten") {

 5             return '你的存在是个错误';

 6         }

 7     },

 8     initialize: function() {

 9         console.log('欢迎来到这个报错的世界!');

10         this.bind('error', function(model, error) {

11             //收到个错误,记录,警告,然后忘记它

12             console.log(error);

13         });

14     }

15 });

16 

17 var person = new Person;

18 person.set({

19     name: 'Mary Poppins',

20     age: -1

21 });

22 

23 //会触发error,输出警告

24 delete person;

25 

26 var person = new Person;

27 person.set({

28     name: 'Dr Manhatten',

29     age: -1

30 })

你可能感兴趣的:(数据)