关于1.0.0版Backbone.js调用validate

网上的调用这个方法的例子都是老版本的,新版本的调用方法有所变化,首先错误绑定事件error换成了invalid,其次设置数据时应传入{validate: true}

var Chapter = Backbone.Model.extend({

  validate: function (attrs) {

    if (attrs.end < attrs.start) {

      return "can't end before it starts";

    }

  }

});



var one = new Chapter({

  title : "Chapter One: The Beginning"

});



one.bind("invalid", function(model, error) {

  alert(model.get("title") + " " + error);

});



one.set({

  start: 15,

  end:   10

}, {validate: true});

 

你可能感兴趣的:(backbone)