Backbone and localStorage

To store Backbone models in localStorage, you can use the ID attribute as key and the serialized data as the value. However, remember that all the data in localStorage is mixed and this strategy will lead to identifier collisions.

Consider that you have two different models (contacts and invoices) with the same ID; when you store one of them in the localStorage, it will overwrite the other.

Another issue with localStorage is that when you want to retrieve data from the store before getting an item from the store, you need to know which key does it have. However, in localStorage, we don't have a clue about what IDs are currently in the store, therefore, we need a way to keep track of the IDs that are in the store at a given ...

Get Mastering Backbone.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.