骨干模型验证不起作用

时间:2013-04-15 14:11:46

标签: backbone.js backbone-events backbone-model

我有这段代码:

this.bindTo(this.model, 'invalid', function(model, error) {
  // this callback is triggered when data is invalid as expected
});

this.model.set("foo", "bar");
this.model.save({}, {
  patch: true,
  wait: true,
  success: function() {

  },
  error: function() {

  }
});

虽然触发了'invalid',但save方法会对服务器进行补丁调用。我认为有一些关于补丁属性的东西,但我不确定。

我的Backbone.js版本是0.9.10。

编辑: 如果我像这样更改我的保存方法它可以工作,但在这种情况下我无法发出补丁请求。

this.save({foo:'bar'},
  //patch: true,
  wait: true,
  ...

编辑: 我的模型是这样的:

aca.Model.User = Backbone.Model.extend(
  _.extend({}, aca.Mixins.Model.User, {
  urlRoot: 'user',
  noIoBind: false,
  socket:window.socket,
  errors:{},
  validations:{
    name:{
      required:true
    }
},
validate:function(changedAttrs) {
  this.errors = Backbone.Validate(this, changedAttrs);
  if(!_.isEmpty(this.errors)) {
    return this.errors;
  }
},
initialize: function () {
  _.bindAll(this, 'serverChange', 'serverDelete', 'modelCleanup');

  if (!this.noIoBind) {
    this.ioBind('update', this.serverChange, this);
    this.ioBind('delete', this.serverDelete, this);
  }
},
serverChange: function (data) {
  data.fromServer = true;
  this.set(data);
},
serverDelete: function (data) {
  if (this.collection) {
    this.collection.remove(this);
  } else {
    this.trigger('remove', this);
  }
  this.modelCleanup();
},
modelCleanup: function () {
  this.ioUnbindAll();
  return this;
},

}) );

0 个答案:

没有答案