Checking the Backbone.js model only on set-> save (not on a selection)

Caution: I work with a backend that I do not have full control over, so I struggle with a few considerations within the Backbone that might be better addressed elsewhere ... Unfortunately, I have no choice but to process them here!

So, my problem is that I really would like to confirm user input from the form (when I set the values ​​with it on the Backbone model), but the models that I get from the API are on newly created objects (via messages that ONLY accept the name and ONLY return the name and identifier of the object) will not conduct verification checks.

As an example, when a new object is created in the database, two key fields are filled as empty lines (and therefore, when the Backbone gets into the API and populates the models, it fills these keys with empty lines). When the user saves these objects back after editing, I would like to force them to enter values ​​for these two keys, which is very simple, given that the basic construction is built into the verification method.

The problem, of course, is that validation works for both fetch-> set (undesirable behavior) and set-> save (desired behavior) - and therefore the newly created models will not load at all ... The trunk collects them, checking not executed, and errors are triggered.

So my question is: is there a "Backbone-y" method only for checking models on set-> save, and not on fetch-> set? Can I use a specific trigger to get around this?

We will be very grateful for any ideas.

+8
source share
2 answers

Backbone.Model.set will not perform the check if you pass in { silent: true } , and fetch will pass any installation parameters so that you can either override fetch or write your own fetchSilent method, which passes this in the options object.

However, you may encounter a small error with Backbone.Collection.fetch , because when it receives attributes from the server, it does not create new models with set . Instead, it creates a new model with model = new this.model(attrs, {collection: this}); , and then checks if the object has a validate method.

This is a little annoying. You can get around it by specifying the parse method in your collection (if you use it) that creates the model silently (using {silent: true} ), because when Backbone.Collection.add gets a fully formed Backbone model, t run the check. (see the _add and _prepareModel in the annotated source ).

It's a little annoying that the collection works that way, but (at least for now) is what it is.

+7
source share

Instead of overriding you can do something else:

When you test your model, check model.silent and confirm it only if it does not exist.

So you want to do the following:

 var test = new MyModel({ id: '123', silent: true }); // in your Model validate function validate: function(attrs) { if (!attrs.silent) { // validate logic here } } 

Then you can choose a model. After you receive your model, you can cancel the silence, for future verification.

+4
source share

All Articles