The LocalStorageProxy uses the new HTML5 localStorage API to save
localStorage is extremely useful for saving user-specific information without needing to build server-side infrastructure to support it. Let's imagine we're writing a Twitter search application and want to save the user's searches locally so they can easily perform a saved search again later. We'd start by creating a Search model:
Our Search model contains just two fields - id and query - plus a Proxy definition. The only configuration we need to
pass to the LocalStorage proxy is an
Saving our data into localStorage is easy and would usually be done with a
//our Store automatically picks up the LocalStorageProxy defined on the Search model var store =
The LocalStorageProxy automatically gives our new Searches an id when we call store.sync(). It encodes the Model data and places it into localStorage. We can also save directly to localStorage, bypassing the Store altogether:
var search =
If this proxy is used in a browser where local storage is not supported, the constructor will throw an error. A local storage proxy requires a unique ID which is used as a key in which all record data are stored in the local storage object.
It's important to supply this unique ID as it cannot be reliably determined otherwise. If no id is provided but the attached store has a storeId, the storeId will be used. If neither option is presented the proxy will throw an error.