I am working on an offline javascript application. It needs to support IE7 so localStorage is not an option. That said, the app does NOT need to persist any information (if you refresh everything gets wiped and that's OK).
So my question is, how do I set Backbone to just use a standard javascript variable (JSON) as my data store?
If I omit the model.url() method I get an error. I imagine this is simple, but I'm not sure what to do.
Thanks!
If you look at what the localStorage adapter is doing, you will find that it is overriding Backbone.sync
. This is the module in Backbone which is responsible for storing/newing/retrieving/updating your data when you call new
, save
, fetch
, etc.
By default, it uses a RESTful endpoint defined in the url
of your model. If you use the LocalStorage override, it puts it in a local store.
If, instead, you just want to put it into an in-memory array, you would just override Backbone.sync
the same way by defining what "read", "update", "create" and "delete" do. I would base it off of the backbone-localstorage.js
adapter since it does most of what you want, but I would then store/retrieve from hash of id/object key/value pairs.
Simply do not use the save
or create
Collection methods.
Instead use store
and add
. These do not attempt to persist the data to storage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With