Underscore.js
to provide 6 object functions on
Backbone.Model
. They aren't all documented here, but you can take a look at the Underscore documentation for the full details…
validate
model.validate(attributes, options)
This method is left undefined, and you're encouraged to override it with your custom validation logic.
By default validate is called before save, but can also be called before set if {validate:true}is passed.
The
validate
method is passed the model attributes, as well as the options from set
or save
. If the attributes are valid, don't return anything from
validate
; if they are invalid, return an error of your choosing.
Failed validations trigger an "invalid"
event, and set the validationError
property on the model with the value returned by this method.
var Chapter = Backbone.Model.extend({ validate: function(attrs, options) { if (attrs.end < attrs.start) { return "can't end before it starts"; } } }); var one = new Chapter({ title : "Chapter One: The Beginning" }); one.on("invalid", function(model, error) { alert(model.get("title") + " " + error); }); one.save({ start: 15, end: 10 });
如果我们想要对Model.set()进行验证,则只需要加上{validate:true}即可,例如:
one.set( { start : 15,end : 10,} , {validate : true} );
validationError
model.validationError
The value returned by validate
during the last failed validation.
one.on('invalid',function(model,error){
alert(model.validationError);
alert(model.get('title')+ ' ' + error);
})
isValid
model.isValid
Run validate
to check the model state.(相当于这个验证有没有返回错误,这样我们就不必要绑定invalid函数了,但是需要记住的是,这里的set就不要加
{validate : true}
了,因为我们是在set后再进行判断的
)
ar Chapter = Backbone.Model.extend({ validate: function(attrs, options) { if (attrs.end < attrs.start) { return "can't end before it starts"; } } }); var one = new Chapter({ title : "Chapter One: The Beginning" }); one.set({ start: 15, end: 10 }); if (!one.isValid()) { alert(one.get("title") + " " + one.validationError); }
url
model.url()
Generates URLs of the form: "[collection.url]/[id]"
by default, but you may override by specifying an explicit urlRoot
if the model's collection shouldn't be taken into account. You can also pass in the model's url
as an option when instantiating it.
Delegates to Collection#url
to generate the URL, so make sure that you have it defined, or a
urlRoot property。
urlRoot
model.urlRoot or model.urlRoot()
Specify a urlRoot
if you're using a model
outside
of a collection, to enable the default
url function to generate URLs based on the model id. "[urlRoot]/id"
var Book = Backbone.Model.extend({urlRoot : '/books'}); var solaris = new Book({id: "1083-lem-solaris"}); alert(solaris.url());
parse
model.parse(response, options)
parse
is called whenever a model's data is returned by the server, in fetch
, and save
.
The function is passed the raw response
object, and should return the attributes hash to be set
on the model.
The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.
clone
model.clone()
Returns a new instance of the model with identical attributes.
var two;
two = one.clone();
console.log(two.toJSON());
isNew
model.isNew()
Has this model been saved to the server yet? If the model does not yet have an id
, it is considered to be new.
hasChanged
model.hasChanged([attribute])
Has the model changed since the last "change"
event? If an
attribute
is passed, returns true
if that specific attribute has changed.
Note that this method, and the following change-related ones, are only useful during the course of a "change"
event.
book.on("change", function() { if (book.hasChanged("title")) { ... } });
changedAttributes
model.changedAttributes([attributes])
Retrieve a hash of only the model's attributes that have changed, or false
if there are none. Optionally, an external
attributes
hash can be passed in, returning the attributes in that hash which differ from the model. This can be used to figure out which portions of a view should be updated, or what calls need to be made to sync the changes to the server.
previous
model.previous(attribute)
During a "change"
event, this method can be used to get the previous value of a changed attribute.
var bill = new Backbone.Model({ name: "Bill Smith" }); bill.on("change:name", function(model, name) { alert("Changed name from " + bill.previous("name") + " to " + name); }); bill.set({name : "Bill Jones"});
previousAttributes
model.previousAttributes()
Return a copy of the model's previous attributes. Useful for getting a diff between versions of a model, or getting back to a valid state after an error occurs.