Backbone.js: run checks and fire events on a set, but don't abort the install if no checks are made

I have a Backbone model with a custom validation method that checks the format of one of the model's attributes. My model is connected to a view that exposes the specified attribute via a textbox. The view has a Save button that the user must explicitly click to save the model changes to the server.

When the user enters an invalid value for an attribute, I want to visually mark that field as invalid. So far it is easy - I can bind the input field change event to the function that calls myModel.set({ attribute: value })

and listen for the event "error"

on the model to signal when the validation failed and I have to mark the input as invalid.

The problem occurs when I want to handle the click of the save button. Because it Backbone.Model.set

overrides the actual setting of the attributes on the model if validation fails, my model will not accurately reflect the value entered by the user if the value is invalid. When the user clicks the Save button after entering an invalid value, I check to see if the model is valid, detects that it is (since the invalid attribute has never been set), and stores the old (valid) attribute value on the server.

What I want seems to be a version set

that always makes the requested changes, but also raises triggers and events. set(..., { silent: true })

will allow the transition to go through, but will not trigger checks or trigger events.

In short, I want my model to sometimes exist in an invalid state (if the user has entered invalid attribute values), and I want to be able to receive events when it transitions between valid and invalid. Is there a neat way to do this with a trunk, or am I thinking about it completely wrong?

+3


source to share


2 answers


What I did with a check like this was to reset the attributes of the models from the inputs before saving on the save click (only doing save if set doesn't work)

Thus, the "Save" button by pressing the re button calls a check - launching an error.



This means that the model is always valid and you cannot go to the next page until all the data is valid.

+2


source


I suggest you use this authentication plugin https://github.com/thedersen/backbone.validation Very helpful. For your use (so that the values ​​are set even if they are not valid), you just need to override the set function of your model.

    set: function (key, value, options) {
        options || (options = {});
        options = _.extend(options, { forceUpdate: true });
        return Backbone.Model.prototype.set.call(this, key, value, options);
    }

      

The forceUpdate property for backbone.validation allows the validate method to return true, but requires validation. After clicking save, you can call,



      var errors = model.validate(model.validate.attributes);

      

This will return any errors your model has and you can display them appropriately. You also have callbacks for valid and invalid which you can use freely

+5


source







All Articles