Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone without a datastore

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!

like image 513
captainclam Avatar asked Jan 30 '12 07:01

captainclam


2 Answers

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.

like image 128
Brian Genisio Avatar answered Nov 10 '22 23:11

Brian Genisio


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.

like image 43
captainclam Avatar answered Nov 11 '22 00:11

captainclam