Update Backbone Model with No Change Events

I recently needed to update an array of models to add a “reflowed” attribute to a comment which would act as a flag to show that the comment had been reordered.

Doing it Wrong

When I was working through this issue, the first thing that came to mind was that I could update the model attributes myself.

This is possible with the following syntax.

model.attributes.attribute = value;

But, this isn’t good software development. So, I trashed that idea.

The Backbone Way

The Backbone way to update a model is:

model.set( { attribute: value } )

But, by default, this will trigger a change event on every changed attribute. For my use case, triggering change events for several models wasn’t necessary.

Looking into the annotated Backbone source code, I noticed that I could pass in an options array that set silent to true. This makes the model update look like this:

model.set( { attribute, value }, { silent: true } );

Leave a Reply

Your email address will not be published. Required fields are marked *