Using Backbone.js, I use Collections to fetch and synchronize data between the client and a Ruby server. That works pretty fine.
Now I would like to use it also to store data in memory (in present case, it would be a collection of articles in a cart).
Is there a way to do this without persisting data on the server nor in the local storage, ie. have the Collection living only in memory?
Whenever I try to set the collection’s url property to a null value, I am getting an Uncaught TypeError: Cannot call method ‘create’ of null. So… I guess this is not the right way to go!
If this not possible, what is the best design to have server and localStorage dealing with different classes in the same application?
In the beginning is easy, just don’t use
fetch()to populate the Collection and usereset()instead. Check reset() documentation.But maybe you start having issues with
Collection.create(),Model.destroy()and so on. So you have to be careful using this methods.. maybe you can useCollection.add()instead ofCollection.create(), but I don’t see good replacement forModel.destroy().I think the most elegant way to solve your problem should be to rewrite the
sync()method for your memory Collection and Model.This is a very guess, written in the fine air:
but maybe it can help you to start working in this direction.
If this is working you still have to use
Collection.reset()instead ofCollection.fetch().