Backbone.js plugins: Local.Storage and Backbone.validation
What is Backbone Local.Storage? Backbone Local.Storage is a method which helps frontend developers to store the data locally. When someone starts making a new application and they need to save some data that they added in their project, and still, they do not have any information downloaded from the backend, they can use Local.Storage.

What is Backbone Local.Storage? Backbone Local.Storage is a method which helps frontend developers to store the data locally. When someone starts a website application development and needs to save some data that they added to their project, and still, do not have any information downloaded from the backend, they can use Local.Storage.
Storing data in Local.Storage may be more secure than storing it in cookies. Furthermore, Local.Storage never transfers data to the server and it has more space (at least 5mb) where we can save our database. Developers can choose if they are willing to store database with it's expiration date (Session Storage - whole database will disappear after closing/refreshing a page) or without, where page can persist it’s refreshing.
How to implement Backbone Local.Storage?
To implement the plugin to our application we just need to download it and add the code below:
tsx
Where our Backbone Collection now looks like this:
tsx
What functions can we use in Backbone Local.Storage?
Local.Storage gives us an opportunity to:
set ("setItem" function, which stores data), get ("getItem" function, which retrieves data from the server) delete items ("removeItem" function, which removes item from the database).
If we want to delete all the data from the server we just need to use clear() function. Local Storage uses "key" values which are stored as strings:
tsx
Local.Storage can be used per one domain. All the pages from the same domain, can access the same database. Also, it is possible to work with Local.Storage without internet connection. Local.Storage allows the fastest access to database and it reduces a number of requests from the server to minimum.
Backbone.Validation
Backbone.Validation is another Backbone.js plugin. It can validate models in your code. Of course, it is also possible to set validation without the plugin, but it will take unnecessarily too much time and too many implemented "if" statements, when Backbone.Validation simplifies our work.
How to implement Backbone.Validation?
Before we implement Backbone.Validation in our app, we need to attach Backbone.js. Afterwards, we are ready to add our plugin. First of all, we need to add this line of code:
tsx
to extend our model with Backbone.Validation plugin. Secondly, in our Backbone.Model we should listed all the attributes we want to validate. All attributes are saved as an object and it should look like this:
tsx
Every attribute owes its properties which have to be fulfilled when we add information to our inputs. In the first attribute, "name" , we’ve put requirement value which is set to true. It says that this field cannot be left empty.
We can add this value to every attribute we use in our validation. In the second attribute we assigned "length" value which is set to 4. It suggests that the zip code should contain only 4 numbers. "Range" value, in the age attribute, informs us that we can set our age from 1 to 80, but this field is not required.
Pattern("email") value underlines that in the email form input we should use only an email form, which will be recognised. In the last example which is difficult, "oneOf" value confines us to only three possibilities which are allowed (["easy", "normal", "hard"]).
We cannot use any other answer because submission will not be accepted. There are many more available values which can be set to validate elements in the input forms (check).
Validation errors
To display validation errors, for example, we can implement bootstrap classes which will style our inputs. It is possible to specify an error message by adding an array of validators like in the example below:
tsx
The msg (message) value can also be a function which returns a string. Validation Binding
Backbone.Validation does not implement any automatic/two-way binding between a model and a view. This is up to you to include it. We can bind view with a model or view with a collection. The first option, binding view with a model, allows us to override Backbone "validate" and "isValid" methods where we can provide some extra features. The view must contain an instance of a model. In the second option, we need a new instance of a collection. Models from our collection can be bound or unbound properly.
Custom validation
If we do not prefer default settings of Backbone.Validation, we can customize it’s logic. Like it is done in the example:
tsx
where "myValidator" is a custom modification. When we set a valid value, we receive nothing, but when the value is invalid it should present an error information. Default error messages or patterns (e.g. email pattern) can also be customised.
Conclusion
I highly recommend both of the plugins. Why do I use them? Because they shorten developer’s work and they are very easy and useful. We can work without backend, so we can create our own database.
Let’s Create a Great Website Together
We'll shape your web platform the way you win it!
More posts in this category
February 05, 2025 • 10 min read
READ MORELearn more about api first cmsAPI-first CMS: What Options You Have (Web Dev Agency's Take)
According to the 2024 State of the API Report by Postman, 74% of organizations are adopting API-first strategies. This statistic isn’t just impressive—it signals a major shift in how businesses operate. While API-first approaches have long been a staple in software development, they're now reshaping content management as well. More and more companies are realizing that traditional CMS platforms can't keep up with the demand for flexibility, speed, and seamless integrations.
January 23, 2025 • 15 min read
READ MORELearn more about best cms for saas top cloud based solutionsBest CMS for SaaS: Top Cloud-Based Solutions
Choosing the right Content Management System (CMS) is a critical decision for your SaaS business. Your unique needs require solutions that are not only flexible, scalable, and user-friendly but also tailored to meet the demands of a fast-paced, customer-focused industry. A CMS should simplify your workflows and help you deliver personalized, high-quality digital experiences.
December 12, 2024 • 10 min read
READ MORELearn more about headless cms for vueWe Picked the Best (Headless) CMS for Vue
Picture a digital experience where content effortlessly flows across platforms, development is agile, and performance is unmatched. By combining the power of Vue.js, a progressive JavaScript framework, with a modern headless CMS, you can achieve all this and more.