[英]Backbone Relational validationError is not return when validation fails
I upgraded my backbone relational library to 0.10.0 I have problems with validating the model, the validationError attribute is undefined in callback method.This attribute has an object in previous version. 我将我的主干关系库升级到0.10.0我在验证模型时遇到问题,在回调方法中未定义validationError属性。此属性在先前版本中有一个对象。
If I changed code in the library file it works but I want to ask if there is a better way or is there a patch for this thanks in advance. 如果我更改了库文件中的代码,它可以工作,但我想问一下是否有更好的方法,或者是否有预先准备的补丁。
I have a model with validate method: 我有一个验证方法的模型:
define( [
'jquery',
'underscore',
'backbone',
'backbonerelational'
], function ( $, _, Backbone, Backbonerelational ) {
'use strict';
var MyModelR = Backbone.RelationalModel.extend( {
idAttribute: "itemId",
defaults: {
_hasServerSideError: false
},
validate: function ( attrs, options ) {
var error;
if ( !attrs.name ) {
error = {
name: 'name',
message: 'Please enter a name.'
};
}
return error ? error : false;
},
url: function () {
return "/myurl";
},
sync: function ( method, model, options ) {
if ( options.action ) {
options.emulateHTTP = true;
options.validate = true;
options.wait = true;
options.url = "/myurl" + options.action;
} else if ( method.toLowerCase() === "update" ) { //default action
options = options || {};
options.url = "/myurl";
}
Backbone.sync( method, model, options );
},
parse: function ( response, options ) {
//code here
}
} );
return MyModelR;
} );
I have a view that listen for the invalid event 我有一个视图,听取无效的事件
myView = Backbone.View.extend( {
initialize: function ( options ) {
this.listenTo( this.MyModelR , 'invalid', this.invalidHandler );
...code
},
invalidHandler: function ( model, error, options ) {
if ( options.validationError ) { //validationError has undefined value
showError(...)
}
});
If I tweak the backbone-relational.js library file at line 1899, it works, I want to ask if there is a better way to solve this or it is a valid defect ? 如果我在第1899行调整了backbone-relational.js库文件,它可以工作,我想询问是否有更好的方法来解决这个问题,或者它是一个有效的缺陷?
if ( model && model.validationError ) {
options.validationError = model.validationError;//Added this line
this.trigger( 'invalid', this, attrs, options );
model = false;
}
https://github.com/jashkenas/backbone/blob/master/backbone.js#L727 https://github.com/jashkenas/backbone/blob/master/backbone.js#L727
The signature is callback(model, error, options)
and options.validationError
is the same as error
. 签名是
callback(model, error, options)
和options.validationError
与error
相同。 Example: 例:
var Album = Backbone.Model.extend({
validate: function(attrs, options) {
if (attrs.artist.toLowerCase() == 'nickelback') {
return "No way you're adding that here";
}
}
});
var album = new Album({artist: 'Nickelback'});
album.on("invalid", function(model, error, options) {
alert(options.validationError);
alert(error); //////////////////////////////////////////// <- look here
});
album.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.