Backbone and Rivets: don't immediately update a model instance with values from DOM

Two-way binding with Rivets and Backbone is great. Your Model instance gets updated as the user introduces changes in a form and then you just have to issue a save() on it.

The thing is that sometimes that Model instance is being shown somewhere else and you don't want the changes to be noticed until the save() operation has been successfully processed (or not) on server-side. In another words, sometimes is desirable to delay publishing of changes from DOM to the Model instance.

The best solution we've thought of is to bind Rivets not to the Model instance itself but to a plain object representation of it (via model.toJSON()). Rivets then publishes changes to that plain object and you only have to pass it as the 'attributes' parameter to model.save().

Have you found a better solution/pattern/practice? Thanks in advance

Answers


I haven't used Rivets per se but one solution to this sort of problem is to use a 'dirty model', ie a copy of the original model that you pass to Rivets and then when you're ready to save it you save it's attributes to the original model. So:

var dirtyModel = new Backbone.Model( originalModel.toJSON() );

then you pass that model to Rivets or whatever else you are using and then you're ready to save it you do:

originalModel.save( dirtyModel.toJSON() );

That way you escape the trap of two-way binding when you don't want it...


Need Your Help

AngularJS - Controller resource call not passing parameters to service factory?

javascript angularjs angularjs-service angular-resource

In AngularJS, I'm trying to pass a parameter in a controller call to a service factory. However, I can't seem to get the parameter passed. The function always passes what I set to be the default in...

PGError: ERROR: permission denied for relation (when using Heroku)

database postgresql heroku permissions

I've recently gone through the database migration process as outlined here: